open android own launcher from my application

前端 未结 2 367
孤城傲影
孤城傲影 2021-02-06 10:30

hi Its been 2 days looking for this simple problem. I want to launch Android own launcher from my application EVEN if its not set as default.

   final PackageMa         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 11:03

    Found the solution, after investigating source code of getLaunchIntentForPackage. As per documentation,

    The current implementation will look first for a main activity in the category CATEGORY_INFO, next for a main activity in the category CATEGORY_LAUNCHER, or return null if neither are found.

    So, the function don't look for CATEGORY_HOME, i rewrote it in following way, it worked perfectly.

    Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
    intentToResolve.addCategory(Intent.CATEGORY_HOME);
    intentToResolve.setPackage("com.android.launcher");
    ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
    if (ri != null) 
    {
        Intent intent = new Intent(intentToResolve);
        intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
    

提交回复
热议问题