为什么找不到活动来处理意图? [英] Why is no Activity found to handle Intent?

查看:78
本文介绍了为什么找不到活动来处理意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望自己创建启动意图,而不是常规的getPackageManager().getLaunchIntentForPackage("com.example.app")方法.

Instead of going the regular getPackageManager().getLaunchIntentForPackage("com.example.app") way, I want to create the launch intent by myself.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage("com.example.app");
startActivity(intent);

如果com.example.app已安装,启用并具有正确的清单,为什么Android找不到Activity? (它与getLaunchIntentForPackage完美配合.)

Why does Android not find the Activity, if the com.example.app is installed, enabled and has a correct manifest? (It works perfectly with getLaunchIntentForPackage.)

推荐答案

我了解您正在尝试启动具有已知程序包名称(com.example.app)的已知应用程序的Launcher活动.我假设您具有有关该应用程序的信息.因此,您可以像这样通过显式意图启动它:

I understand that you are trying to start the Launcher activity of a known application with known package name (com.example.app). I assume you have information about the application. Thus, you can start it via explicit intent like so:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app", "com.example.app.MainActivity"));
if(intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

对两个意图对象(intent1 ==您自己的意图VS intent2 ==从getLaunchIntentForPackage()创建的意图)进行分析,区别是

Doing an analysis of the two intent objects (intent1 == your own intent VS intent2 == intent created from getLaunchIntentForPackage()), the difference is

意图1:

{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] pkg = com.example.app}

{ act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=com.example.app }

意图2:

{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10000000 pkg = com.example.app cmp = com.example.app/.MainActivity}

{ act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.example.app cmp=com.example.app/.MainActivity }

我将不得不相信,创建自己的意图对象所做的工作不足以使显式意图发挥作用.您将必须向Android提供有关您的意图的更多信息,例如特定于组件名称(如我上面的回答所示).

I will have to believe that what you have done to create your own intent object is not enough for the explicit intent to work. You will have to provide Android more information about your intent such as being specific with the component name (as shown in my answer above).

这篇关于为什么找不到活动来处理意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆