How to check if a service is running on Android?

2018-12-31 02:29发布

How do I check if a background service (on Android) is running?

I want an Android activity that toggles the state of the service -- it lets me turn it on if it is off and off if it is on.

23条回答
看风景的人
2楼-- · 2018-12-31 03:04

Below is an elegant hack that covers all the Ifs. This is for local services only.

    public final class AService extends Service {

        private static AService mInstance = null;

        public static boolean isServiceCreated() {
            try {
                // If instance was not cleared but the service was destroyed an Exception will be thrown
                return mInstance != null && mInstance.ping();
            } catch (NullPointerException e) {
                // destroyed/not-started
                return false;
            }
        }

        /**
         * Simply returns true. If the service is still active, this method will be accessible.
         * @return
         */
        private boolean ping() {
            return true;
        }

        @Override
        public void onCreate() {
            mInstance = this;
        }

        @Override
        public void onDestroy() {
            mInstance = null;
        }
    }

And then later on:

    if(AService.isServiceCreated()){
        ...
    }else{
        startService(...);
    }
查看更多
春风洒进眼中
3楼-- · 2018-12-31 03:05

onDestroy isn't always called in the service so this is useless!

For example: Just run the app again with one change from Eclipse. The application is forcefully exited using SIG: 9.

查看更多
栀子花@的思念
4楼-- · 2018-12-31 03:06

You can use this (I didn't try this yet, but I hope this works):

if(startService(someIntent) != null) {
    Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();
}
else {
    Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
}

The startService method returns a ComponentName object if there is an already running service. If not, null will be returned.

See public abstract ComponentName startService (Intent service).

This is not like checking I think, because it's starting the service, so you can add stopService(someIntent); under the code.

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 03:06
    public boolean checkServiceRunning(){
         ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
                {
                    if ("com.example.yourpackagename.YourServiceName"
                            .equals(service.service.getClassName())) 
                    {
                        return true;
                    }
                }
             return false;
    }
查看更多
美炸的是我
6楼-- · 2018-12-31 03:06

Again, another alternative that people might find cleaner if they use pending intents (for instance with the AlarmManager:

public static boolean isRunning(Class<? extends Service> serviceClass) {
    final Intent intent = new Intent(context, serviceClass);
    return (PendingIntent.getService(context, CODE, intent, PendingIntent.FLAG_NO_CREATE) != null);
}

Where CODE is a constant that you define privately in your class to identify the pending intents associated to your service.

查看更多
登录 后发表回答