How to redirect User to default launcher in an android app?

依然范特西╮ 提交于 2020-01-01 16:49:39

问题


It's been now a long time since I try to figure out how to develop a android unlock screen app working with an NFC authentication. I am working with a Nexus S. After several researches on the Internet, I came to the conclusion that replacing the lock screen as a third party app is just not supported right now and that we need to modify the platform to do a decent implementation.

This is the reason why I developed an home screen app that give the illusion of an unlockscreen app. But my problem is that I would like to be redirect to the default laucher at the end of the procedure.

I first tried with the code below but the problem is that I will have the choice between the default home screen and my new app

Intent goHome = new Intent();
i.setAction("android.intent.action.MAIN");
i.addCategory("android.intent.category.HOME");
startActivity(goHome);

So I then I tried with the code below but it doesn't seem to work.

Intent goHome = new Intent(Intent.ACTION_MAIN);
goHome.setClassName("com.android.launcher", "com.android.launcher.Launcher");
startActivity(goHome);

I get the following logCat

Unable to find explicit activity class ( com.android.launcher.laucher)

Do you have any idea why it doesn't work? Is it the right way of doing it?


回答1:


try this :

goHome.setClassName("com.android.launcher", "com.android.launcher2.Launcher");



回答2:


Use:

    PackageManager pm = getPackageManager();
    Intent i = new Intent("android.intent.action.MAIN");
    i.addCategory("android.intent.category.HOME");
    List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
    if (lst != null) {
       for (ResolveInfo resolveInfo : lst) {
           try {
           Intent home = new Intent("android.intent.action.MAIN");
           home.addCategory("android.intent.category.HOME");
           home.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
           startActivity(home);
           break;
           } catch (Throwable t) {
               t.printStackTrace();
           }
       }
    }



回答3:


In case you have a Samsung device, the following code worked for me:

Intent goHome = new Intent(Intent.ACTION_MAIN);
        goHome.setClassName("com.sec.android.app.launcher", "com.android.launcher2.Launcher");
        startActivity(goHome);


来源:https://stackoverflow.com/questions/9787658/how-to-redirect-user-to-default-launcher-in-an-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!