关闭

关于android 应用中安装另一个应用 打开另一个应用 判断另一个应用是否安装

标签: android
781人阅读 评论(0) 收藏 举报
分类:

转自:http://blog.csdn.net/jiaruoranshuai/article/details/48677375

1.在一个安卓应用中安装另一个android应用

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(tempApk),"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.android.packageinstaller",    "com.android.packageinstaller.PackageInstallerActivity");
ctx.startActivity(intent);

2.获取一个apk文件的包名

PackageManager pm = context.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(Constant.DOWNLOAD_DIR + "/" + functionalls.get(i).getId() + ".apk", PackageManager.GET_ACTIVITIES);//第一个参数是apk文件的路径
ApplicationInfo appInfo = null;
if (info != null) {
    appInfo = info.applicationInfo;
    String packageName = appInfo.packageName;    
}

3.启动另一个android应用(需要知道包名)

private void doStartApplicationWithPackageName(String packagename) {

    // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
    PackageInfo packageinfo = null;
    try {
        packageinfo = context.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<ResolveInfo> resolveinfoList = context.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);
    }
}


4.判断一个apk文件是否已经安装成功(根据报名判断该应用是否已经成功安装)

private boolean isAppInstalled(String packageName){
    PackageManager pm = context.getPackageManager();
    boolean installed =false;
    try{
        pm.getPackageInfo(uri,PackageManager.GET_ACTIVITIES);
        installed =true;
    }catch(PackageManager.NameNotFoundException e){
        installed =false;
    }
    return installed;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:159172次
    • 积分:2208
    • 等级:
    • 排名:第18045名
    • 原创:21篇
    • 转载:257篇
    • 译文:0篇
    • 评论:23条
    文章分类