前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android极速开发之Apk安装

Android极速开发之Apk安装

作者头像
Javen
发布2018-08-21 10:58:07
6530
发布2018-08-21 10:58:07
举报
文章被收录于专栏:酷玩时刻酷玩时刻

安装之前判断是否有root权限,如果有root权限就静默安转,如果没有就利用意图进行安装。源码参考地址

代码语言:javascript
复制
/**
 * 
 * @author Javen
 * @since 2016/05/24
 */
public class ApkController {
    /**
     * 描述: 安装
     */
    public static boolean install(String apkPath,Context context){
        // 先判断手机是否有root权限
        if(hasRootPerssion()){
            // 有root权限,利用静默安装实现
            return clientInstall(apkPath);
        }else{
            // 没有root权限,利用意图进行安装
            File file = new File(apkPath);
            if(!file.exists())
                return false; 
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
            context.startActivity(intent);
            return true;
        }
    }
     
    /**
     * 描述: 卸载
     */
    public static boolean uninstall(String packageName,Context context){
        if(hasRootPerssion()){
            // 有root权限,利用静默卸载实现
            return clientUninstall(packageName);
        }else{
            Uri packageURI = Uri.parse("package:" + packageName);
            Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
            uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(uninstallIntent);
            return true;
        }
    }
     
    /**
     * 判断手机是否有root权限
     */
    private static boolean hasRootPerssion(){
        PrintWriter PrintWriter = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.flush();
            PrintWriter.close();
            int value = process.waitFor();  
            return returnResult(value);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        return false;
    }
     
    /**
     * 静默安装
     */
    private static boolean clientInstall(String apkPath){
        PrintWriter PrintWriter = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.println("chmod 777 "+apkPath);
            PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
            PrintWriter.println("pm install -r "+apkPath);
//          PrintWriter.println("exit");
            PrintWriter.flush();
            PrintWriter.close();
            int value = process.waitFor();  
            return returnResult(value);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        return false;
    }
     
    /**
     * 静默卸载
     */
    private static boolean clientUninstall(String packageName){
        PrintWriter PrintWriter = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
            PrintWriter.println("pm uninstall "+packageName);
            PrintWriter.flush();
            PrintWriter.close();
            int value = process.waitFor();  
            return returnResult(value); 
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(process!=null){
                process.destroy();
            }
        }
        return false;
    }
     
    /**
     * 启动app
     * com.exmaple.client/.MainActivity
     * com.exmaple.client/com.exmaple.client.MainActivity
     */
    public static boolean startApp(String packageName,String activityName){
        boolean isSuccess = false;
        String cmd = "am start -n " + packageName + "/" + activityName + " \n";
        Process process = null;
        try {
           process = Runtime.getRuntime().exec(cmd);
           int value = process.waitFor();  
           return returnResult(value);
        } catch (Exception e) {
          e.printStackTrace();
        } finally{
            if(process!=null){
                process.destroy();
            }
        }
        return isSuccess;
    }
     
     
    private static boolean returnResult(int value){
        // 代表成功  
        if (value == 0) {
            return true;
        } else if (value == 1) { // 失败
            return false;
        } else { // 未知情况
            return false;
        }  
    }
    
    /**
     * 查询所有非系统app的信息
     * @param mContext
     * @return
     */
    public static List<Map<String, Object>> getAPPInstalled(Context mContext) {

        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

        // 获取系统内的所有程序信息
        Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
        mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<PackageInfo> packageinfo = mContext.getPackageManager()
                .getInstalledPackages(0);

        int count = packageinfo.size();
        for (int i = 0; i < count; i++) {

            PackageInfo pinfo = packageinfo.get(i);
            ApplicationInfo appInfo = pinfo.applicationInfo;
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                // 系统程序 忽略
            } else {
                // 非系统程序
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("app_logo", pinfo.applicationInfo.loadIcon(mContext
                        .getPackageManager()));
                map.put("app_name", pinfo.applicationInfo.loadLabel(mContext
                        .getPackageManager()));
                map.put("package_name", pinfo.applicationInfo.packageName);
                map.put("app_version_name", pinfo.versionName);
                map.put("app_version_code", pinfo.versionCode);

                listItems.add(map);
            }
        }
        return listItems;
    }
    
    
    /**
     * 判断应用是否需要安装
     * @param mContext
     * @param packageName
     * @param versionCode
     * @return
     */
    public static boolean isInstalled(Context mContext,String packageName,int versionCode) {

        // 获取系统内的所有程序信息
        Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
        mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<PackageInfo> packageinfo = mContext.getPackageManager()
                .getInstalledPackages(0);

        int count = packageinfo.size();
        String pn;
        int vc;
        for (int i = 0; i < count; i++) {

            PackageInfo pinfo = packageinfo.get(i);
            ApplicationInfo appInfo = pinfo.applicationInfo;
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                // 系统程序 忽略
            } else {
                // 非系统程序
                pn=pinfo.applicationInfo.packageName;
                vc=pinfo.versionCode;

                if (pn.equalsIgnoreCase(packageName) && vc >= versionCode) {
                    return true;
                }
            }
        }
        return false;
    }
}

获取手机安装的非系统应用

代码语言:javascript
复制
public class App {

    public static List<Map<String, Object>> getAPPInstalled(Context mContext) {

        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

        //查询所有非系统app的信息
        Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
        mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<PackageInfo> packageinfo = mContext.getPackageManager()
                .getInstalledPackages(0);

        int count = packageinfo.size();
        for (int i = 0; i < count; i++) {

            PackageInfo pinfo = packageinfo.get(i);
            ApplicationInfo appInfo = pinfo.applicationInfo;
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                // 系统程序 忽略
            } else {
                // 非系统程序
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("app_logo", pinfo.applicationInfo.loadIcon(mContext
                        .getPackageManager()));
                map.put("app_name", pinfo.applicationInfo.loadLabel(mContext
                        .getPackageManager()));
                map.put("package_name", pinfo.applicationInfo.packageName);
                map.put("app_version_name", pinfo.versionName);
                map.put("app_version_code", pinfo.versionCode);

                listItems.add(map);
            }
        }
        return listItems;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.08.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档