search

Home  >  Q&A  >  body text

android - 应用市场打开已经安装的APP,直接进入到这个APP当前页面是怎么做的??

应用市场打开已经安装的APP,直接进入到这个APP当前页面是怎么做的??

怪我咯怪我咯2772 days ago489

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 17:41:14

    Start by package name

    private void doStartApplicationWithPackageName(String packagename) {  
      
        // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等  
        PackageInfo packageinfo = null;  
        try {  
            packageinfo = getPackageManager().getPackageInfo(packagename, 0);  
        } catch (NameNotFoundException e) {  
            e.printStackTrace();  
        }  
        if (packageinfo == null) {  
            return;  
        }  
      
        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent  
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);  
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
        resolveIntent.setPackage(packageinfo.packageName);  
      
        // 通过getPackageManager()的queryIntentActivities方法遍历  
        List<ResolveInfo> resolveinfoList = getPackageManager()  
                .queryIntentActivities(resolveIntent, 0);  
      
        ResolveInfo resolveinfo = resolveinfoList.iterator().next();  
        if (resolveinfo != null) {  
            // packagename = 参数packname  
            String packageName = resolveinfo.activityInfo.packageName;  
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]  
            String className = resolveinfo.activityInfo.name;  
            // LAUNCHER Intent  
            Intent intent = new Intent(Intent.ACTION_MAIN);  
            intent.addCategory(Intent.CATEGORY_LAUNCHER);  
      
            // 设置ComponentName参数1:packagename参数2:MainActivity路径  
            ComponentName cn = new ComponentName(packageName, className);  
      
            intent.setComponent(cn);  
            startActivity(intent);  
        }  
    }  

    If you need to switch applications, you need to use getRunningTasks to get the running application and determine whether the package name to be opened is in this list. If it is, use the moveTaskToFront method of activityManager to move the application to the top of the stack

         ActivityManager mAm = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);  
            List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(100);  
            for (ActivityManager.RunningTaskInfo rti : taskList) {  
                //找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台  
                if(rti.topActivity.getPackageName().equals(context.getPackageName())) {  
                    mAm.moveTaskToFront(rti.id,0);  
                    return;  
                }  
            }  

    reply
    0
  • PHPz

    PHPz2017-04-17 17:41:14

    Questioner: Have you solved this problem? ? It seems that there is a bug in the method test above

    reply
    0
  • Cancelreply