• PackageUtils


    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.net.Uri;
    import java.io.File;
    public class PackageUtils {
       protected static final String TAG = PackageUtils.class.getSimpleName();
    
       /**
        * 安装apk应用
        */
       public static void installAPK(Context context, File apkFile) {
          if (apkFile.isFile()) {
             String fileName = apkFile.getName();
             String postfix = fileName.substring(fileName.length() - 4, fileName.length());
             if (postfix.toLowerCase().equals(".apk")) {
                String cmd = "chmod 755 " + apkFile.getAbsolutePath();
                try {
                   Runtime.getRuntime().exec(cmd);
                } catch (Exception e) {
                   Logger.e(TAG, e.getLocalizedMessage());
                }
                Uri uri = Uri.fromFile(apkFile);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
                context.startActivity(intent);
             }
          } else if (apkFile.isDirectory()) {
             File[] files = apkFile.listFiles();
             int fileCount = files.length;
             for (int i = 0; i < fileCount; i++) {
                installAPK(context, files[i]);
             }
          }
       }
    
       /**
        * 安装apk应用
        */
       public static void installDirApk(Context context, String filePath) {
          File file = new File(filePath);
          installAPK(context, file);
       }
    
       /**
        * 卸载apk文件
        */
       public static void uninstallPackage(Context context, Uri packageUri) {
          Intent intent = new Intent(Intent.ACTION_DELETE, packageUri);
          context.startActivity(intent);
       }
    
       /**
        * 根据包得到应用信息
        */
       public static ApplicationInfo getApplicationInfoByName(Context context, String packageName) {
          if (null == packageName || "".equals(packageName)) {
             return null;
          }
          try {
             return context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
          } catch (NameNotFoundException e) {
             Logger.e("EspanceUtils", packageName + " NameNotFoundException");
             return null;
          }
       }
    
       /**
        *  通过包名获取包信息
        */
       public static PackageInfo getPackageInfoByName(Context context, String packageName) {
          if (null == packageName || "".equals(packageName)) {
             return null;
          }
          try {
             return context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
          } catch (NameNotFoundException e) {
             Logger.e(TAG, e.getLocalizedMessage());
             return null;
          }
       }
    
       /**
        * 判断apk包是否安装
        */
       public static boolean isApkIntalled(Context context, String packageName) {
          if (null == getApplicationInfoByName(context, packageName)) {
             return false;
          } else {
             return true;
          }
       }
    }
    
  • 相关阅读:
    everything is nothing
    基础算法
    OC 优化目录
    iOS 更改启动视图
    单例--iOS
    OC-Objection 学习笔记之一:简单的开始
    iOS 类库列表
    IOS 上线问题
    OC强弱引用的使用规则
    设置桌面图标
  • 原文地址:https://www.cnblogs.com/loaderman/p/6435094.html
Copyright © 2020-2023  润新知