以编程方式启动默认的 Android 启动器
我正在寻找一种以编程方式启动默认 Android 启动器的方法, 可能类似于下面的代码。或者我必须在清单文件中添加一些内容吗? 谢谢!
Intent intent = new Intent();
intent.setClassName("com.android.launcher", "Launcher");
startActivity(intent);
I'm looking for a way to launch the default android launcher programatically,
something perhaps like the code below. Or do I have to add something to the manifest file?
Thanks!
Intent intent = new Intent();
intent.setClassName("com.android.launcher", "Launcher");
startActivity(intent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你试过这个吗?
(我自己没有尝试过,因为我的用例有点复杂——我更换了启动器,我想调用旧启动器...)
我已经还发现您可以使用包管理器来查看满足某些意图过滤条件的所有活动。例如,如果您想查找标记为主页默认主页活动的所有活动,请使用以下命令:
请注意,我已替换了设备上的默认主屏幕 - 这就是为什么我必须确保我找到的活动不是正在运行的活动!如果您尚未替换默认的家庭活动,则不需要此检查 - 只需使用第一个(也可能是唯一的)默认家庭活动。
(请注意,我仍然无法从我的启动器启动旧启动器,也许是因为旧启动器保留了默认启动器(这是我的新启动器)的记录,并且只是回调它。我不知道。但是在至少它不会崩溃,而且我猜想,如果您没有更换旧的主屏幕,它可能会起作用。)
Have you tried this?
(I haven't tried it myself, because my use case is a little more complicated---I've replaced the launcher, and I want to call the old launcher...)
I've also discovered that you can use the package manager to look through all activities that meet some intent filter criteria. For example, if you want to find all the activities marked as the home default home activity, use this:
Note that I have replaced the default home screen on my device---that's why I have to make sure the activity I found is not the activity that's running! If you haven't replaced the default home activity, you don't need this check---just use the first (and probably the only) default home activity.
(Note that I still can't launch the old launcher from my launcher, perhaps because the old launcher keeps a record of the default launcher, which is my new launcher, and simply calls back to it. I don't know. But at least it doesn't crash, and I would guess that, if you haven't replaced the old home screen, it just might work.)
根据 Garret Wilson 的回答,这里有一个丑陋的单行,假设
context
是您的应用程序上下文:此代码假设原始系统主活动始终是
queryIntentActivities
返回的第一个结果,而接受的答案返回不属于正在运行的包的第一个主活动。目前还不清楚如何干净地获取系统主活动。有些线程提到可以使用 getPackageManager().resolveActivity(intent, flags) 来实现此目的,但似乎 PackageManager.MATCH_SYSTEM_ONLY 不能与此方法一起使用。
Following Garret Wilson's answer, here's an ugly one-liner, assuming
context
is your application context:This code assumes that the original system home activity is always the first result returned by
queryIntentActivities
, whereas the accepted answer returns the first home activity not belonging to the running package.It's still unclear how to cleanly get the system home activity. Some threads mention that
getPackageManager().resolveActivity(intent, flags)
can be used for this, but it seemsPackageManager.MATCH_SYSTEM_ONLY
cannot be used with this method.此代码可以打开应用程序启动器,
This code can open the app launcher,