编程方式启动默认的Andr​​oid发射 [英] Launching default android launcher programmatically

查看:132
本文介绍了编程方式启动默认的Andr​​oid发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方法以编程方式启动默认的Andr​​oid发射器, 也许东西像下面的code。还是我的东西添加到清单文件? 谢谢!

 意向意图=新的意图();
intent.setClassName(com.android.launcher,启动器);
startActivity(意向);
 

解决方案

您是否尝试过这个?

  startActivity(新意图(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER));
 

(我还没有尝试过自己,因为我的用例是更复杂一点---我已经取代了发射台,我想叫的的发射器...)

我还发现,您可以使用软件包管理器浏览所有满足一些意向性的过滤条件的活动。例如,如果你想找到所有标记为Home默认主页活动活动,用这个:

 意向意图= NULL;
最终PackageManager packageManager = getPackageManager();
对于(最终ResolveInfo resolveInfo:packageManager.queryIntentActivities(新意图(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME),PackageManager.MATCH_DEFAULT_ONLY))
{
    如果(!getPackageName()。等于(resolveInfo.activityInfo.packageName))//如果这个活动是不是在我们的活动(换句话说,它的另一个默认主屏幕)
    {
        意图= packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName));
        打破;
    }
}
 

请注意,我已经取代了默认主屏幕设备上 - 这就是为什么我必须确保我发现活动是不是在运行活动!如果你还没有取代默认的家庭活动,则不需要此检查---只使用第一个(可能是唯一)的默认主页的活动。

(请注意,我仍然无法从我的启动启动旧发射器,也许是因为旧的启动保持默认的启动,这是我的新发射台,只是回调到它的记录。我不知道,但至少它不会崩溃,我猜,如果你还没有取代了旧的主屏幕,它只是可能会奏效。)

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

解决方案

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

这篇关于编程方式启动默认的Andr​​oid发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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