android开发之在一个APP启动另一个APP

private void openApp(String packageName) {
        PackageInfo pi = null;
        try {
            pi = getActivity().getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);

        List<ResolveInfo> apps = getActivity().getPackageManager().queryIntentActivities(resolveIntent, 0);

        ResolveInfo ri = apps.iterator().next();
        if (ri != null ) {
            packageName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            ComponentName cn = new ComponentName(packageName, className);

            intent.setComponent(cn);
            startActivity(intent);
        }
    }


直接调用方法,输入要启动的应用的包名就Ok

阅读更多
文章标签: android
个人分类: android开发
上一篇android开发之 java.lang.NoClassDefFoundError:retrofit2.Retrofit$Builder 错误解决
下一篇android开发之获取手机所有应用的包名
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭