4

I noticed that some running services have a Settings rather than Stop button in the details page of Running services in the system Settings app. I want to setup my own service to work like this.

After digging into the source code of Settings app, I found a clue:

ActivityManager.getRunningServiceControlPanel()

Returns a PendingIntent you can start to show a control panel for the given running service. If the service does not have a control panel, null is returned.

My question is: how can I set a control panel for my own service?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kevin Yuan
  • 1,008
  • 8
  • 20

1 Answers1

3

In case anyone's curious, I found the code behind this feature. The service's description and configuration intent can be set during a service binding, if and only if the caller is running as SYSTEM.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/com/android/server/am/ActivityManagerService.java#11651

        int clientLabel = 0;
        PendingIntent clientIntent = null;

        if (callerApp.info.uid == Process.SYSTEM_UID) {
            // Hacky kind of thing -- allow system stuff to tell us
            // what they are, so we can report this elsewhere for
            // others to know why certain services are running.
            try {
                clientIntent = (PendingIntent)service.getParcelableExtra(
                        Intent.EXTRA_CLIENT_INTENT);
            } catch (RuntimeException e) {
            }
            if (clientIntent != null) {
                clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0);
                if (clientLabel != 0) {
                    // There are no useful extras in the intent, trash them.
                    // System code calling with this stuff just needs to know
                    // this will happen.
                    service = service.cloneFilter();
                }
            }
        }

This code was moved at some point, but still exists in KitKat, unchanged.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/com/android/server/am/ActiveServices.java#665

j__m
  • 9,392
  • 1
  • 32
  • 56