Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a "control panel" for my Service in Android?

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?

like image 263
Kevin Yuan Avatar asked Oct 29 '25 09:10

Kevin Yuan


1 Answers

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

like image 109
j__m Avatar answered Oct 31 '25 11:10

j__m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!