-1

I want to check if service is running or not after the application is force closed by user. How can I do this. I am doin this in onReceive of broadcastReceiver. But even after force closing it shows service running Toast.

   boolean isServiceRunning = AppSettings.getServiceRunning(context);       
    if (isServiceRunning)
    {
        Toast.makeText(context,"service running", Toast.LENGTH_LONG).show();

    } 
    else 
    {   
        Toast.makeText(context,"service stopped", Toast.LENGTH_LONG).show();        
    }
Manasi
  • 348
  • 1
  • 4
  • 14

1 Answers1

1

Try this:

public boolean checkRunningService() {
        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo myService : activityManager
                .getRunningServices(Integer.MAX_VALUE)) {
            if ("com.main.YOUR_SERVICE_NAME".equals(myService.service
                    .getClassName())) {
                return true;
            }
        }
        return false;
    }

Hope this helps.

Edit:-

Your service in manifest file should be like this:

<service
            android:name="com.main.YOUR_SERVICE_NAME"
            android:enabled="true" />
Ravi Sharma
  • 873
  • 7
  • 24