1

I am using an Activity called "FirstStart" in my Android Application. After the user press a "register" Button, I start a registration intent service to register the user on my server and on GCM. In my onClik(View v) I start the Intent:

Intent intent = new Intent(this, RegistrationIntentService.class);
        intent.putExtra("user", givenUsername);
        startService(intent);

After this, the public void onHandleIntent(Intent intent) starts and get a token from GCM and put this to my server.

This process needs a few seconds. I dont want the user to wait on the normal screen, so I put a progress bar in the FirstStart Activity. It is called and started before the RegistrationIntentService starts.

After the registration intent service finished, I want to stop the progress bar in FirstStart and finish the registration intent service and also the FirstStart Activity.

So how can I finish the FirstStart Activity at the end of my RegistrationIntentService? Or is there a way to go back into the FirstStart Activity, when the RegistrationIntentService is finished?

mhellmeier
  • 1,982
  • 1
  • 22
  • 35
  • please Google ResultReceiver class – Nguyễn Hoài Nam Jan 29 '16 at 00:11
  • 4
    It sounds like the best option for you is to bind the Service to your Activity. Take a look at this example, see that it calls `bindService()`, and look at the `Callbacks` interface in the service, and the `updateClient()` implementation in the Activity: http://stackoverflow.com/questions/20594936/communication-between-activity-and-service – Daniel Nugent Jan 29 '16 at 00:58

1 Answers1

2

So how can I finish the FirstStart Activity at the end of my RegistrationIntentService?

Obviously a communication between FirstStart and the RegistrationIntentService must be established. The fastest way to achieve it would be to:

  • register BroadcastReceiver in the FirstStart by using registerReceiver() method. Secondly, once all the job within the RegistrationIntentService is done, the only thing you have to do is to sendBroadcast() from the Service. Remember about the IntentFilter with proper action set so that the broadcast sent from the RegistrationIntentService will match registered BroadcastReceiver in FirstStart
  • Do basically the same thing as in the first suggestion but by using LocalBroadcastManager The distinction between regular broadcasting and broadcasting with the LocalBroadcastManager is that in the latter case broadcast messages are visible only for your application's process. In the former one, the Broadcast may be intercepted by different application (if no permission is restricted ofc)
  • Use some open-source goods like green robot's EventBus which does basically technically the same thing in terms of broadcasting but with custom Objects you create.

Or is there a way to go back into the FirstStart Activity, when the RegistrationIntentService is finished?

Still, broadcast-based communication is the way you make the Activity speak with Service.

Hope that helps somehow:)

dawid gdanski
  • 2,432
  • 3
  • 21
  • 29
  • 1
    Thanks a lot. The LocalBroadcastManager solves my communication problem. I also found this nice example code for a LocalBroadcastManager: https://gist.github.com/Antarix/8131277 – mhellmeier Jan 29 '16 at 20:09