Android: Understanding Using One IntentService With Multiple Activities

I have a problem with intent not to be delivered to IntentService

under a specific activity flow: This is the scenario:

  • Consider 3 actions: Home, B and C. C has 2 fragments CF1 and CF2.
  • B, CF1, and CF2 use the same IntentService class, but with different actions.
  • The IntentService is started using startService(Intent)

    . (getActivity (). startService (Intent) for fragments)
  • In cases where the IntentService is started, I am pretty sure it will be stopped using stopService(intent)

    if it is started in the Activity / Fragment onStop()

    .
  • If the activity stream is Home → C → CF1 → CF2, everything works.
  • If the activity stream is Home → B → C → CF1 → CF2, then it is onHandleIntent

    never called after starting the service (Intent) from CF2. B and CF1 are processed. For debugging, I tried to wait for the IntentService in Activity B to finish and then go to CF1 -> CF2, still the same problem. CF1 never has any problem starting the same service. When I tried to create a new class IntentService

    for CF2 it worked.

I figured out that IntentService has a queue for intents. If the service starts for the first time, the onStartCommand is called (which we shouldn't handle for the IntentService). If the service is already running, onHandleIntent is called for each subsequent call to startService.

Obviously I am doing something wrong, but it is not clear how what. I tried looking into other stackoverflow questions but didn't help. The code I'm using is pretty simple:

AndroidManifest.xml

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

      

Activity B

@Override
public void onCreate(Bundle savedInstanceState)
{
       .......
       intent = new Intent(getApplicationContext(), ExampleIntentService.class);
       intent.setAction(StringConstants.ACTION_B);
       serviceRunning = true; //set to false in onReceiveResult 
       startService(intent);
}

@Override
public void onStop()
{
      if(serviceRunning && intent != null)
          stopService(intent)
}

      

Fragment CF1

@Override
public void onResume()
{
    super.onResume();

    intent = new Intent(getActivity(), ExampleIntentService.class);
    intent.setAction(StringConstants.ACTION_CF1);
    serviceRunning = true; //set to false in onReceiveResult 
    startService(intent);
}

@Override
public void onStop()
{
      if(serviceRunning && intent != null)
          stopService(intent)
}

      

The code is exactly the same for the snippet, CF2

+3


source to share


1 answer


My understanding was ... If the service is started for the first time, the onStartCommand is called (which we shouldn't handle for the IntentService). If the service is already running, onHandleIntent is called for each subsequent call to startService.

Not. onStartCommand()

called for every call startService()

. onHandleIntent()

called for every call startService()

made to IntentService

, unless you do something in onStartCommand()

to change the normal behavior.

The IntentService is started using startService (Intent).

You send commands to IntentService

using startService()

.



In cases where the IntentService is started I am pretty sure it is terminated with stopService (intent)

This is an extremely bad idea. IntentService

will stop when all calls startService()

have been processed as stated in the documentation :

The IntentService will receive Intents, start the worker thread, and stop serving as needed.

+6


source







All Articles