android在只拥有第三方apk的情况下在自己app中打开第三方app

 
前提是第三方apk的AndroidManifest的某个activity中的export = "true"   
/**
     *
     * 打开另一个app
     * @param packageName
     * @param className
     */
    public void goToAnotherApp(String packageName,String className){
        Intent intent = new Intent();
        if(isAvilible(Main2Activity.this,packageName)){
            intent.setAction(Intent.ACTION_MAIN);
            //前提:知道要跳转应用的包名、类名
            ComponentName componentName = new ComponentName(packageName, className);
            intent.setComponent(componentName);
            startActivity(intent);
        }else{
            Toast.makeText(Main2Activity.this, "没有该应用", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 判断相对应的APP是否存在
     *
     * @param context
     * @param packageName(包名)(若想判断QQ,则改为com.tencent.mobileqq,若想判断微信,则改为com.tencent.mm)
     * @return
     */
    public boolean isAvilible(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();

        //获取手机系统的所有APP包名,然后进行一一比较
        List pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (((PackageInfo) pinfo.get(i)).packageName
                    .equalsIgnoreCase(packageName))
                return true;
        }
        return false;
    }

    /**
     * 通过已知apk获取到该app的包名
     */
    private void getPackageNameByApk() {
        String FilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "xinga.apk";//输入APK地址
        PackageManager pm = getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(FilePath, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            String appName = pm.getApplicationLabel(appInfo).toString();
            String packageName = appInfo.packageName;  //获取安装包名称
//            com.travelsky.mrt.oneetrip
//            com.travelsky.mrt.oneetrip.common.WelcomeActivity
            Log.i("Abel_Test", "包名是:" + packageName);
            String version = info.versionName; //获取版本信息
            Log.i("Abel_Tes", "版本信息:" + version);
        }
    }


    /**
     * 通过包名获取app详细信息
     * @param packagename
     */
    private void doStartApplicationWithPackageName(String packagename) {
        // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.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 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;
            Log.i("Abel_Tes", "版本信息:" + className);
            // 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);
        }
    }

 

你可能感兴趣的:(Android)