0

I have an Intent service & a BroadcastReceiver.

As per background limitation on Android Oreo & above, the background applications(when an application is not foreground ) cannot use the started service. When you call startService() method from the background applications simply through the IllegalStateException.

But In my case, My intent service is running properly even when the app is in the background. I am using ADB cmd to trigger broadcast.

Please correct where I am missing.

adb shell am broadcast -a android.intent.action.TEST --es maxCountValue 10 -n com.example.servicedemo/.MyReceiver
enter code here

BroadcastReceiver class

class MyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Toast.makeText(context, "CompleteReceiver", Toast.LENGTH_LONG).show()
        if (intent!!.action.equals("android.intent.action.TEST")) {
            val mIntent = Intent(context, MyIntentService::class.java).apply {
                Log.v("MyIntentService", intent.data.toString())
                this.putExtra("maxCountValue", 100)
            }
            context?.startService(mIntent)
        }
    }
}

Intent Service

private const val SERVICE_NAME = "MyIntentService"

class MyIntentService : IntentService(SERVICE_NAME) {
    private val handler = Handler()

    override fun onCreate() {
        super.onCreate()
        showToast("Job Execution Started")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        showToast("Job Execution onStartCommand")
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        showToast("Job Execution onDestroy")
    }
    override fun onHandleIntent(intent: Intent?) {
        val maxCount = intent!!.getIntExtra("maxCountValue", -1)

        for (i in 0 until maxCount) {
            Log.d(SERVICE_NAME, "onHandleWork: The number is: $i")
            try {
                Thread.sleep(100)
            } catch (e: InterruptedException) {
                Log.d(SERVICE_NAME, "Exception: ")
                e.printStackTrace()
            }
        }
    }

    private fun showToast(msg: String) {
        handler.post {
            Toast.makeText(this@MyIntentService, msg, Toast.LENGTH_LONG).show()
        }
    }
}

Manifest :

<service android:name=".MyIntentService"/>

        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.TEST" />
            </intent-filter>
        </receiver>
Kishan Maurya
  • 3,356
  • 8
  • 21

1 Answers1

0

"The definition of background for purposes of service limitations is distinct from the definition used by memory management; an app might be in the background as pertains to memory management, but in the foreground as pertains to its ability to launch services."

-

https://developer.android.com/about/versions/oreo/background

PS that's the whole purpose of services