0

I want to start service when I exit from my app.

code is :

Intent i_service   = new Intent(MainActivity.this,check.class);
startService(i_service); 

And when I re-open my app , I want to stop my service which I started when I exit from my app.

code is:

 Intent i_service   = new Intent(MainActivity.this,check.class);
 stopService(i_service); 

This is the right way to do it ?

How can stop my old service?

thanks in advance

CompEng
  • 7,161
  • 16
  • 68
  • 122

2 Answers2

1
Check Your service is run or not and then stop ur service on oncreate() of ur activity as below,



  ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (check.getName().equals(service.service.getClassName())) {
           Intent i_service   = new Intent(MainActivity.this,check.class);
 stopService(i_service); 
        }
    }
1

I think you are doing it in the correct way. Just that when the Application is started again, you need to first check whether it is running or not, and then stop it.

The answer with code described by "MSS" above explains it well!

Jim C
  • 1,785
  • 1
  • 13
  • 13