从AndroidManifest中读取服务名称

问题描述 投票:0回答:1

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

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

android android-service android-manifest
1个回答
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
        }
    }

我很快就会添加文档

© www.soinside.com 2019 - 2024. All rights reserved.