直到一个月前,我才成功地从我的另一个应用程序使用intent-filter开始服务.

应用A:

 <service
            android:name="com.example.app.MyService"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.test.START" />
            </intent-filter>
</service>

应用程序B:

public static Intent createExplicitIntent(Context context, Intent intent) {
    List<ResolveInfo> queryIntentServices = context.getPackageManager().queryIntentServices(intent, 0);
    System.out.println(queryIntentServices.size());

    if (queryIntentServices.size() != 1) {
        return null;
    }

    ResolveInfo resolveInfo = queryIntentServices.get(0);
    ComponentName componentName = new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
    Intent intent2 = new Intent(intent);
    intent2.setComponent(componentName);
    return intent2;
}

然后,最后是:

 Intent intent = new Intent("com.test.START");
 createExplicitIntent(context, intent);

但由于某种原因,它停止了工作.我不确定我做了什么改变, destruct 了这种方法.

推荐答案

如果你想让你的代码在Android 11和更高版本上运行,你需要在App B中使用<queries>元素,因为你需要declare your package visibility needs.

在你的情况下,这样的东西可能会起作用:

    <queries>
        <intent>
            <action android:name="com.test.START" />
        </intent>
    </queries>

Java相关问答推荐

Gmail Javi API批量处理太多请求

基于仅存在于父级中的字段查询子文档?

Character::Emoji不支持带数字的字符吗?

JVM会优化这个数学运算吗?

当我已经安装了其他版本的Java时,如何在Mac OSX 14.3.1上安装Java 6?

我不能再在Android Studio Hedgehog上用Java语言创建新项目了吗?

在Spring Boot应用程序中导致";MediaTypeNotSupportdException&qot;的映像上载

Jolt变换JSON数组问题

在添加AdMob时无法为Google Play构建应用程序包:JVM垃圾收集器崩溃和JVM内存耗尽

Java 21中泛型的不兼容更改

JXBrowser是否支持加载Chrome扩展?

接受类及其接口的Java类型(矛盾)

在Java Spring JPA中插入包含对其他实体的引用的列

无法在IntStream上应用Collectors.groupingBy

模拟JUnit未检测到返回字符串的方法的任何声纳覆盖

根据应用程序 Select 的语言检索数据

由于版本不匹配,从Java 8迁移到Java 17和Spring 6 JUnit4失败

如何在Java中正确实现填字游戏求解器

为什么 log4j 过滤器在appender中不起作用

Spring Boot应用程序中的自定义constraintvalidator不会被调用