Android 从我自己的应用程序启动外国应用程序

发布于 2024-11-29 13:02:16 字数 300 浏览 1 评论 0原文

我正在尝试执行以下代码:

Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_HOME);
intent.setComponent(new ComponentName(" **Home package** "," **Home class** "));
                  startActivity(intent);

本质上,我正在寻找一种专门定位和加载准确的原始家庭应用程序的方法。

I am trying to get the following code to execute:

Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_HOME);
intent.setComponent(new ComponentName(" **Home package** "," **Home class** "));
                  startActivity(intent);

Essentially I am looking for a way to specifically target and load the exact, original, home application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-12-06 13:02:16

从技术上讲,您无法始终了解“准确的、原始的、家庭应用程序”。

您可以使用 PackageManagerqueryIntentActivities() 来查找所有响应 MAIN/HOME Intents 的人。如果有两个答案,而你的答案是一个(我猜这就是你的情况),那么另一个答案基本上就是“精确的、原始的、家庭应用程序”。您可以通过获取与已解析活动关联的 ApplicationInfo 对象并检查 FLAG_SYSTEM 以查看它是否安装在系统映像中来进一步验证这一点。这种方法可能并不完全安全,但它可能足以满足您的需求。

还有一种选择是,您可以在第一次运行时简单地记录当前默认的 MAIN/HOME 活动。在用户选择将您设置为默认应用程序之前,您的应用程序很有可能会运行。同样,这也有漏洞(例如,它们在第一次运行您之前将您设置为默认值)。

Technically, you have no way of always knowing "the exact, original, home application".

You can use PackageManager and queryIntentActivities() to find find who all responds to MAIN/HOME Intents. If there are two answers, and yours is one (which I am guessing is your situation), then the other is "the exact, original, home application" pretty much by definition. You can further verify this by getting to the ApplicationInfo object associated with the resolved activity and checking for FLAG_SYSTEM to see if it is installed in the system image. This approach is probably not completely bulletproof, but it may be close enough for your needs.

Yet another option is for you to simply record the current default MAIN/HOME activity when you are run for the first time. Odds are decent that your application will be run before the user elects to make you the default. Again, this has holes (e.g., they make you the default before running you for the first time).

能否归途做我良人 2024-12-06 13:02:16

编辑:解决方案:

        PackageManager pm=getPackageManager();
        Intent main=new Intent(Intent.ACTION_MAIN, null);

        main.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

        Collections.sort(launchables,
                         new ResolveInfo.DisplayNameComparator(pm));

        int launcher_flag = findLauncherApp(launchables);

        ResolveInfo launchable = launchables.get(launcher_flag);

        ActivityInfo activity=launchable.activityInfo;
        ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                             activity.name);
        Intent i=new Intent(Intent.ACTION_MAIN);

        i.addCategory(Intent.CATEGORY_LAUNCHER);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        i.setComponent(name);

        startActivity(i);

findLaucherApp() 将列表转换为字符串数组并询问每个字符串以查看它是否包含“com.android.launcher2”,然后返回其 id。

EDIT: SOLUTION:

        PackageManager pm=getPackageManager();
        Intent main=new Intent(Intent.ACTION_MAIN, null);

        main.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

        Collections.sort(launchables,
                         new ResolveInfo.DisplayNameComparator(pm));

        int launcher_flag = findLauncherApp(launchables);

        ResolveInfo launchable = launchables.get(launcher_flag);

        ActivityInfo activity=launchable.activityInfo;
        ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                             activity.name);
        Intent i=new Intent(Intent.ACTION_MAIN);

        i.addCategory(Intent.CATEGORY_LAUNCHER);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        i.setComponent(name);

        startActivity(i);

Where findLaucherApp() turns the List into an array of strings and interrogates each one to see if it contains "com.android.launcher2" and then returns its id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文