我想在我的项目中像InstanceId service
一样写Firebase的service
。该项目是一个SDK
,集成它的开发人员可以覆盖这个service
。在这种情况下,我应该能够读取开发人员在其AndroidManifest.xml
文件中使用特定操作指定的新服务的名称。
所以真正的问题是,如何通过特定的操作读取AndroidManifest.xml
文件中声明的服务名称?
使用以下实用方法
public static void startService(Context context, String lookupAction) {
Intent serviceIntent = new Intent();
serviceIntent.setAction(lookupAction);
serviceIntent.setPackage("package.of.your.application");
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(serviceIntent, 0);
if (resInfo != null && !resInfo.isEmpty()) {
ServiceInfo service = resInfo.get(0).serviceInfo;
ComponentName cmpService = new ComponentName(service.applicationInfo.packageName, service.name);
Intent serviceToStart = new Intent(lookupAction);
serviceToStart.setComponent(cmpService);
context.startService(serviceToStart);
} else {
// Handle error
}
}
我很快就会添加文档