我注意到一些正在运行的服务在系统设置应用程序的Running services
的详细信息页面中有Settings
而不是Stop
按钮。我想设置我自己的服务像这样工作。
在深入研究了设置应用程序的源代码后,我发现了一个线索:
ActivityManager.getRunningServiceControlPanel ()
返回一个PendingIntent,你可以开始显示给定运行服务的控制面板。如果服务没有控制面板,则返回null。
我的问题是:如何为我自己的服务设置control panel
?
如果有人好奇,我找到了这个特性背后的代码。服务的描述和配置意图可以在服务绑定期间设置,当且仅当调用者作为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();
}
}
}
这段代码在某个时候被移动了,但仍然存在于KitKat中,没有改变。
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/com/android/server/am/ActiveServices.java 665