zoukankan      html  css  js  c++  java
  • 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;
          }
       }
    }
    
  • 相关阅读:
    Pyhton学习-Python与中间件之Celery(7)
    Pyhton学习-Python与中间件之RabbitMQ(6)
    Pyhton学习-Python与中间件之Redis(5)
    Pyhton学习-Python操作数据库之Redis(3)
    解决pycharm问题:module 'pip' has no attribute 'main'
    C#调用Python脚本打印pdf文件
    C# 三种打印方式含代码
    python-memcached学习笔记
    django框架使用mysql步骤
    pycharm配置运行django项目步骤
  • 原文地址:https://www.cnblogs.com/loaderman/p/6435094.html
Copyright © 2011-2022 走看看