Android 从我自己的应用程序启动外国应用程序
我正在尝试执行以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上讲,您无法始终了解“准确的、原始的、家庭应用程序”。
您可以使用
PackageManager
和queryIntentActivities()
来查找所有响应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
andqueryIntentActivities()
to find find who all responds toMAIN
/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 theApplicationInfo
object associated with the resolved activity and checking forFLAG_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).编辑:解决方案:
findLaucherApp() 将列表转换为字符串数组并询问每个字符串以查看它是否包含“com.android.launcher2”,然后返回其 id。
EDIT: SOLUTION:
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.