content: package cn.hotapk.fastandrutils.utils;
  
  import android.app.ActivityManager;
  import android.app.ActivityManager.RunningServiceInfo;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  import android.content.pm.PackageManager.NameNotFoundException;
  import android.net.Uri;
  import android.text.TextUtils;
  
  import java.security.MessageDigest;
  import java.util.List;
  
  /**
   * @author laijian
   * @version 2017/9/20
   * @Copyright (C)下午4:06 , www.hotapk.cn
   * app相关信息
   */
  public final class FAppUtils {
      private FAppUtils() {
  
      }
  
      /**
       * 获取App包名
       *
       * @return App包名
       */
      public static String getAppPackageName() {
          return FUtils.getAppContext().getPackageName();
      }
  
      /**
       * 获取App名称
       *
       * @return App名称
       */
      public static String getAppName() {
          return getAppName(getAppPackageName());
      }
  
      /**
       * 获取App名称
       *
       * @param packageName 包名
       * @return App名称
       */
      public static String getAppName(final String packageName) {
          if (TextUtils.isEmpty(packageName)) return null;
          try {
              PackageManager pm = FUtils.getAppContext().getPackageManager();
              PackageInfo pi = pm.getPackageInfo(packageName, 0);
              return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString();
          } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
              return null;
          }
      }
  
      /**
       * 得到软件版本号
       *
       * @return 当前版本Code
       */
      public static int getVerCode() {
          int verCode = -1;
          try {
              String packageName = FUtils.getAppContext().getPackageName();
              verCode = FUtils.getAppContext().getPackageManager().getPackageInfo(packageName, 0).versionCode;
          } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
          }
          return verCode;
      }
  
      /**
       * 得到软件显示版本信息
       *
       * @return 当前版本信息
       */
      public static String getVerName() {
          String verName = "";
          try {
              String packageName = FUtils.getAppContext().getPackageName();
              verName = FUtils.getAppContext().getPackageManager().getPackageInfo(packageName, 0).versionName;
          } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
          }
          return verName;
      }
  
      /**
       * 获取应用签名
       *
       * @param pkgName 包名
       */
      public static String getSign(String pkgName) {
          try {
              PackageInfo pis = FUtils.getAppContext().getPackageManager().getPackageInfo(
                      pkgName, PackageManager.GET_SIGNATURES);
              return hexdigest(pis.signatures[0].toByteArray());
          } catch (NameNotFoundException e) {
              e.printStackTrace();
              return null;
          }
      }
  
      /**
       * 将签名字符串转换成需要的32位签名
       *
       * @param paramArrayOfByte 签名byte数组
       * @return 32位签名字符串
       */
      private static String hexdigest(byte[] paramArrayOfByte) {
          final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97,
                  98, 99, 100, 101, 102};
          try {
              MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
              localMessageDigest.update(paramArrayOfByte);
              byte[] arrayOfByte = localMessageDigest.digest();
              char[] arrayOfChar = new char[32];
              for (int i = 0, j = 0; ; i++, j++) {
                  if (i >= 16) {
                      return new String(arrayOfChar);
                  }
                  int k = arrayOfByte[i];
                  arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
                  arrayOfChar[++j] = hexDigits[(k & 0xF)];
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return "";
      }
  
  
      /**
       * 安装apk
       *
       * @param file APK文件uri
       */
      public static void installApk(Uri file) {
          Intent intent = new Intent();
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.setAction(Intent.ACTION_VIEW);
          intent.setDataAndType(file, "application/vnd.android.package-archive");
          FUtils.getAppContext().startActivity(intent);
      }
  
      /**
       * 卸载apk
       *
       * @param packageName 包名
       */
      public static void uninstallApk(String packageName) {
          Intent intent = new Intent(Intent.ACTION_DELETE);
          Uri packageURI = Uri.parse("package:" + packageName);
          intent.setData(packageURI);
          FUtils.getAppContext().startActivity(intent);
      }
  
      /**
       * 检测服务是否运行
       *
       * @param className 类名
       * @return 是否运行的状态
       */
      public static boolean isServiceRunning(String className) {
          boolean isRunning = false;
          ActivityManager activityManager = (ActivityManager) FUtils.getAppContext()
                  .getSystemService(Context.ACTIVITY_SERVICE);
          List<RunningServiceInfo> servicesList = activityManager
                  .getRunningServices(Integer.MAX_VALUE);
          for (RunningServiceInfo si : servicesList) {
              if (className.equals(si.service.getClassName())) {
                  isRunning = true;
              }
          }
          return isRunning;
      }
  
      /**
       * 停止运行服务
       *
       * @param context   上下文
       * @param className 类名
       * @return 是否执行成功
       */
      public static boolean stopRunningService(Context context, String className) {
          Intent intent_service = null;
          boolean ret = false;
          try {
              intent_service = new Intent(context, Class.forName(className));
          } catch (Exception e) {
              e.printStackTrace();
          }
          if (intent_service != null) {
              ret = context.stopService(intent_service);
          }
          return ret;
      }
  
  }
  