1

Possible Duplicate:
android: check if a service is running

I have one services to perform my action, it will run on background indefinite time. I want to monitor the service whether its is running or not. So, I want to create another service to monitor first one.

Is There any other action interfilter to broadcast, service running or not?

Community
  • 1
  • 1
kumar_android
  • 2,263
  • 1
  • 21
  • 30
  • "it will run on background indefinite time" -- why? Users generally do not like this, which is why users will get rid of your services via task killers, "Force Stop" in Settings, etc. – CommonsWare Nov 23 '12 at 13:01
  • @CommonsWare If the user 'force stop' the services, if it is possible to get the notification? – kumar_android Nov 23 '12 at 13:09
  • Absolutely not. By force-stopping your app, the user is indicating that the user does not want your app to run anymore, because your app is ill-behaved. Your process is immediately terminated, and you are not notified of this fact. So, I ask again: why do you have a service that "will run on background indefinite time"? – CommonsWare Nov 23 '12 at 13:10
  • @CommonsWare I want to block camera process in one service and want to watch another service whether it is running or not. If not i want to re-start the service again – kumar_android Nov 23 '12 at 14:12
  • 1
    "I want to block camera process in one service" -- talented programmers use the device admin APIs to control access to the camera, rather than playing childish script-kiddie games with processes and everlasting services. – CommonsWare Nov 23 '12 at 14:15
  • Of course, Admin APIs for disabling camera will work on Android ICS. For the lower version of android, am doing these workaround. – kumar_android Nov 24 '12 at 05:16

2 Answers2

0

You can use getRunningServices(int max) of ActivityManager

ActivityManager actManager = // get the activity manager

int max = ??;

List<ActivityManager.RunningServiceInfo> runningServices = actManager.getRunningServices(max);

for(ActivityManager.RunningServiceInfo runningService : runningServices)
{

   boolean started = runningService.started;
}

Note: this method is only intended for debugging or implementing service management type user interfaces.

  • I want to know, is there any notification available when service is stopped?..or else i want to check this method in services in indfinite time? – kumar_android Nov 23 '12 at 12:53
0
Presumably, it is calling getRunningServices() on ActivityManager. There is no documented Intent to go straight to that screen.

Step 1. First you need to declare and initialize few variables :-

private static final String APP_DETAILS_PACKAGE_NAME = “com.android.settings”; // Here you need to define the  package name

private static final String SCREEN_CLASS_NAME = “com.android.settings.RunningServices”; // Here you need to define the class name but NOTICE!! you need to define its full name including  package name.

Step 2. Instantiate an Intent

Intent intent = new Intent();

Step 3. Set Action to ACTION_VIEW

intent.setAction(Intent.ACTION_VIEW);

Step 3. Set class name inside the intent as we know that package can have more than one activity. So Intent needs something to match the Activity inside the package name.

intent.setClassName(APP_DETAILS_PACKAGE_NAME,  SCREEN_CLASS_NAME);

Step 4. Start the Activity

context.startActivity(intent);

In above example if you want to access some different screen then change the APP_DETAILS_PACKAGE_NAME and SCREEN_CLASS_NAME as per your need.

Jatinkumar Patel
  • 1,693
  • 2
  • 15
  • 35