以编程方式启动默认的 Android 启动器

发布于 2024-11-24 17:28:40 字数 210 浏览 6 评论 0原文

我正在寻找一种以编程方式启动默认 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 技术交流群。

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

发布评论

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

评论(5

呆° 2024-12-01 17:28:40

你试过这个吗?

startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER));

(我自己没有尝试过,因为我的用例有点复杂——我更换了启动器,我想调用启动器...)

我已经还发现您可以使用包管理器来查看满足某些意图过滤条件的所有活动。例如,如果您想查找标记为主页默认主页活动的所有活动,请使用以下命令:

Intent intent=null;
final PackageManager packageManager=getPackageManager();
for(final ResolveInfo resolveInfo:packageManager.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY))
{
    if(!getPackageName().equals(resolveInfo.activityInfo.packageName))  //if this activity is not in our activity (in other words, it's another default home screen)
    {
        intent=packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName));
        break;
    }
}

请注意,我已替换了设备上的默认主屏幕 - 这就是为什么我必须确保我找到的活动不是正在运行的活动!如果您尚未替换默认的家庭活动,则不需要此检查 - 只需使用第一个(也可能是唯一的)默认家庭活动。

(请注意,我仍然无法从我的启动器启动旧启动器,也许是因为旧启动器保留了默认启动器(这是我的新启动器)的记录,并且只是回调它。我不知道。但是在至少它不会崩溃,而且我猜想,如果您没有更换旧的主屏幕,它可能会起作用。)

Have you tried this?

startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER));

(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:

Intent intent=null;
final PackageManager packageManager=getPackageManager();
for(final ResolveInfo resolveInfo:packageManager.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY))
{
    if(!getPackageName().equals(resolveInfo.activityInfo.packageName))  //if this activity is not in our activity (in other words, it's another default home screen)
    {
        intent=packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName));
        break;
    }
}

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.)

谷夏 2024-12-01 17:28:40

根据 Garret Wilson 的回答,这里有一个丑陋的单行,假设 context 是您的应用程序上下文:

context.startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(context.getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY).get(0).activityInfo.packageName));

此代码假设原始系统主活动始终是 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:

context.startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(context.getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY).get(0).activityInfo.packageName));

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 seems PackageManager.MATCH_SYSTEM_ONLY cannot be used with this method.

檐上三寸雪 2024-12-01 17:28:40
=> In kotlin add below code in onDestroy method of appCompactActvity use to make your app as default launcher, 

override fun onDestroy() {
        var intent = Intent(Intent.ACTION_MAIN)
        var packageManager: PackageManager = packageManager
        for (resolveInfo in packageManager.queryIntentActivities(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY)) {
            if (packageName != resolveInfo.activityInfo.packageName)  //if this activity is not in our activity (in other words, it's another default home screen)
            {
                startActivity(intent)
            }
            break
        }
        super.onDestroy()
    }
=> In kotlin add below code in onDestroy method of appCompactActvity use to make your app as default launcher, 

override fun onDestroy() {
        var intent = Intent(Intent.ACTION_MAIN)
        var packageManager: PackageManager = packageManager
        for (resolveInfo in packageManager.queryIntentActivities(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY)) {
            if (packageName != resolveInfo.activityInfo.packageName)  //if this activity is not in our activity (in other words, it's another default home screen)
            {
                startActivity(intent)
            }
            break
        }
        super.onDestroy()
    }
话少情深 2024-12-01 17:28:40
 startActivity( Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
 startActivity( Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
甩你一脸翔 2024-12-01 17:28:40

此代码可以打开应用程序启动器,

   
Intent intentf = new Intent(Intent.ACTION_ALL_APPS);
intentf.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentf);

This code can open the app launcher,

   
Intent intentf = new Intent(Intent.ACTION_ALL_APPS);
intentf.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentf);

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