Search code examples
androidandroid-servicetext-to-speech

How to check whether TtsService (or an Android service) is stopped?


For reasons beyond my app's control, TtsService is stopped by the system, providing only Log.i() hints in LogCat:

05-01 12:01:55.662: INFO/TtsService(1791): Stopping
05-01 12:01:55.662: INFO/TtsService(1791): Stopped

I would like to be able to handle this situation from within my app.

Is there a way (a callback or a system call) to detect when this happens?

If not, is there a way to check whether the TTS service is running?


Solution

  • You can use following method to check whether service is running or not.

    ServiceName = "package.service".\\service name
    public static boolean isServiceRunning(Context ctx, String serviceName) 
        {
            ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
            for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                if (serviceName.equalsIgnoreCase(service.service.getClassName())) {
                    Log.v(TAG,serviceName+": Service running");
                    return true;
                }
            }
            return false;
        }