Anand
Anand

Reputation: 1959

Stopping a service after Intent -Android

In my android application, I have an activity called A .From A, I call a background service.In that services onStartCommand I will do some AsyncTask and Provided an Intent to move to another activity called B. Where I am stuck is,

1. How can I stop the service itself after calling the Intent?

EDIT: I tried calling StopSelf(); but it not ending my service.

2. After reaching B the back button should not work. Currently if press backbutton I can still go to Activity A.

My Intent

Intent dialogIntent = new Intent(UserLoginValidationService.this,SplashScreen.class);                                            
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                       
                        stopSelf();
                        startActivity(dialogIntent);

EDIT: How to call finishAffinity(); in service?

Any help is appreciated.

Upvotes: 0

Views: 150

Answers (2)

Jay Kumar Panera
Jay Kumar Panera

Reputation: 146

This could be the best way to do it.

  1. Create an Intent Service.
  2. Register a Local BroadcastReceiver in your A Activity from where you started the Service.
  3. Broadcast from your Intent Service and Receive it in you A Activity.
  4. Do the needful in that BroadcastReceiver code.

NOTE: unregister BroadcastReceiver in A Activity onDestroy() method.

stopSelf() do stop the service but it could take little time. You can cross check it by putting log in onDestroy() Method of Service.

Upvotes: 7

ViramP
ViramP

Reputation: 1709

1. How can I stop the service itself after calling the Intent?

You can stop service using stopSelf().

2. After reaching B the back button should not work. Currently if press backbutton I can still go to Activity A.

If you want to close the application on back button press then you should open at Activity B clearing previous activity.

Upvotes: 1

Related Questions