从AndroidManifest中读取服务名称

时间:2017-12-27 07:08:06

标签: android android-service android-manifest

我想在我的项目中编写Firebase的InstanceId service,如service。该项目是SDK,集成它的开发人员可以覆盖此service。在这种情况下,我应该能够通过AndroidManifest.xml文件中的特定操作读取开发人员指定的新服务的名称。

所以真正的问题是,如何通过特定操作读取AndroidManifest.xml文件中声明的服务名称?

1 个答案:

答案 0 :(得分:0)

使用以下实用方法

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
        }
    }

我将尽快添加文档