前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XYLibrary笔记八:PackageManager基础

XYLibrary笔记八:PackageManager基础

作者头像
项勇
发布2023-03-24 13:37:51
3260
发布2023-03-24 13:37:51
举报
文章被收录于专栏:项勇

一.代码位置(点击末尾阅读原文)

  • xlib-Application
  • XTester-packagemanger

二.引用方式

将JitPack存储库添加到您的构建文件中 将其添加到存储库末尾的root build.gradle中:

代码语言:javascript
复制
allprojects {
      repositories {
      ...
      maven { url 'https://jitpack.io' }
      }
}

添加依赖项

代码语言:javascript
复制
dependencies {
      ...
      implementation 'com.github.itemuse:XLib:Tag'
}

初始化 Application中init

代码语言:javascript
复制
import cn.xy.library.XApp;
      ...
@Override
public void onCreate() {
      super.onCreate();
      XApp.init(this);
}

AndroidManifest.xml中添加UID

代码语言:javascript
复制
android:sharedUserId="android.uid.system"

三.几个方法

判断APP是否安装

代码语言:javascript
复制
/**
 * Return whether the app is installed.
 *
 * @param pkgName The name of the package.
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isAppInstalled(@NonNull final String pkgName) {
    PackageManager packageManager = XApp.getApp().getPackageManager();
    try {
        return packageManager.getApplicationInfo(pkgName, 0) != null;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}

判断APP是否DEBUG版本

代码语言:javascript
复制
/**
  * Return whether it is a debug application.
  *
  * @param packageName The name of the package.
  * @return {@code true}: yes<br>{@code false}: no
  */
 public static boolean isAppDebug(final String packageName) {
     if (isSpace(packageName)) return false;
     try {
         PackageManager pm = XApp.getApp().getPackageManager();
         ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
         return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
     } catch (PackageManager.NameNotFoundException e) {
         e.printStackTrace();
         return false;
     }
 }

判断APP是否系统应用

代码语言:javascript
复制
/**
 * Return whether it is a system application.
 *
 * @param packageName The name of the package.
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isAppSystem(final String packageName) {
    if (isSpace(packageName)) return false;
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}

判断APP是否正在运行

代码语言:javascript
复制
/**
 * Return whether application is running.
 *
 * @param pkgName The name of the package.
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isAppRunning(@NonNull final String pkgName) {
    int uid;
    PackageManager packageManager = XApp.getApp().getPackageManager();
    try {
        ApplicationInfo ai = packageManager.getApplicationInfo(pkgName, 0);
        if (ai == null) return false;
        uid = ai.uid;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
    ActivityManager am = (ActivityManager) XApp.getApp().getSystemService(Context.ACTIVITY_SERVICE);
    if (am != null) {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(Integer.MAX_VALUE);
        if (taskInfo != null && taskInfo.size() > 0) {
            for (ActivityManager.RunningTaskInfo aInfo : taskInfo) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    if (pkgName.equals(aInfo.baseActivity.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        List<ActivityManager.RunningServiceInfo> serviceInfo = am.getRunningServices(Integer.MAX_VALUE);
        if (serviceInfo != null && serviceInfo.size() > 0) {
            for (ActivityManager.RunningServiceInfo aInfo : serviceInfo) {
                if (uid == aInfo.uid) {
                    return true;
                }
            }
        }
    }
    return false;
}

获取APP图标

代码语言:javascript
复制
/**
 * Return the application's icon.
 *
 * @param packageName The name of the package.
 * @return the application's icon
 */
public static Drawable getAppIcon(final String packageName) {
    if (isSpace(packageName)) return null;
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.applicationInfo.loadIcon(pm);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

获取APP名

代码语言:javascript
复制
/**
 * Return the application's name.
 *
 * @param packageName The name of the package.
 * @return the application's name
 */
public static String getAppName(final String packageName) {
    if (isSpace(packageName)) return "";
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

获取APP路径

代码语言:javascript
复制
/**
 * Return the application's path.
 *
 * @param packageName The name of the package.
 * @return the application's path
 */
public static String getAppPath(final String packageName) {
    if (isSpace(packageName)) return "";
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.applicationInfo.sourceDir;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

获取APP版名

代码语言:javascript
复制
/**
 * Return the application's version name.
 *
 * @param packageName The name of the package.
 * @return the application's version name
 */
public static String getAppVersionName(final String packageName) {
    if (isSpace(packageName)) return "";
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? null : pi.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

获取APP版本号

代码语言:javascript
复制
/**
 * Return the application's version code.
 *
 * @param packageName The name of the package.
 * @return the application's version code
 */
public static int getAppVersionCode(final String packageName) {
    if (isSpace(packageName)) return -1;
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        return pi == null ? -1 : pi.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return -1;
    }
}

获取APP签名

代码语言:javascript
复制
/**
 * Return the application's signature.
 *
 * @param packageName The name of the package.
 * @return the application's signature
 */
public static Signature[] getAppSignature(final String packageName) {
    if (isSpace(packageName)) return null;
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        @SuppressLint("PackageManagerGetSignatures")
        PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        return pi == null ? null : pi.signatures;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

获取APP的UI-ID

代码语言:javascript
复制
/**
 * Return the application's user-ID.
 *
 * @param pkgName The name of the package.
 * @return the application's signature for MD5 value
 */
public static int getAppUid(String pkgName) {
    try {
        ApplicationInfo ai = XApp.getApp().getPackageManager().getApplicationInfo(pkgName, 0);
        if (ai != null) {
            return ai.uid;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

获取APP信息

代码语言:javascript
复制
/**
 * Return the application's information.
 * <ul>
 * <li>name of package</li>
 * <li>icon</li>
 * <li>name</li>
 * <li>path of package</li>
 * <li>version name</li>
 * <li>version code</li>
 * <li>is system</li>
 * </ul>
 *
 * @param packageName The name of the package.
 * @return the application's information
 */
public static AppInfo getAppInfo(final String packageName) {
    try {
        PackageManager pm = XApp.getApp().getPackageManager();
        if (pm == null) return null;
        return getBean(pm, pm.getPackageInfo(packageName, 0));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
  1. 获取/设置APP启用/禁用状态
    • 设置
代码语言:javascript
复制
/**
 * @param packageName to hidden or show app
 * @param enable true ENABLED ; false DISABLED
 * need android:sharedUserId="android.uid.system"
 */
public void changeAppState(String packageName, boolean enable){
    try {
        PackageManager packageManager = XApp.getApp().getPackageManager();
        if(enable){
            packageManager.setApplicationEnabledSetting(packageName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,0);
        }else{
            packageManager.setApplicationEnabledSetting(packageName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,0);
        }
    } catch (Exception e) {
        XLog.debug(e.getMessage());
    }
}

获取

代码语言:javascript
复制
  /**
 * @return
 * -1 no intalled
 *  1 ENABLED
 *  2 DISABLED
 *
 */
public int getAppState(String packageName) {
    try {
        PackageManager packageManager = XApp.getApp().getPackageManager();
        return packageManager.getApplicationEnabledSetting(packageName);
    } catch (Exception e) {
        XLog.debug(e.getMessage());
        return -1;
    }
}
  1. 获取/设置Activity启用/禁用状态
    • 获取
代码语言:javascript
复制
/**
 * @return
 * -1 no intalled
 *  1 ENABLED
 *  2 DISABLED
 */
public int getActivityState(ComponentName componentName){
    try {
        PackageManager packageManager = XApp.getApp().getPackageManager();
        return packageManager.getComponentEnabledSetting(componentName);
    } catch (Exception e){
        return -1;
    }
}

设置

代码语言:javascript
复制
/**
 * @param componentName to hidden or show activity
 * @param enable true ENABLED ; false DISABLED
 * need android:sharedUserId="android.uid.system"
 */
public void changActivityState(ComponentName componentName,boolean enable){
    try {
        PackageManager packageManager = XApp.getApp().getPackageManager();
        if (enable){
            packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,0);
        }else {
            packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,0);
        }
    } catch (Exception e){
        XLog.debug(e.getMessage());
    }
}

ComponentName mComponentName

代码语言:javascript
复制
mComponentName = new ComponentName("cn.xy.windowmanager","cn.xy.windowmanager.MainActivity")
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.代码位置(点击末尾阅读原文)
  • 二.引用方式
  • 三.几个方法
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档