将JitPack存储库添加到您的构建文件中 将其添加到存储库末尾的root build.gradle中:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
添加依赖项
dependencies {
...
implementation 'com.github.itemuse:XLib:Tag'
}
初始化 Application中init
import cn.xy.library.XApp;
...
@Override
public void onCreate() {
super.onCreate();
XApp.init(this);
}
AndroidManifest.xml中添加UID
android:sharedUserId="android.uid.system"
判断APP是否安装
/**
* 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版本
/**
* 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是否系统应用
/**
* 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是否正在运行
/**
* 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图标
/**
* 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名
/**
* 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路径
/**
* 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版名
/**
* 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版本号
/**
* 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签名
/**
* 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
/**
* 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信息
/**
* 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;
}
}
/**
* @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());
}
}
获取
/**
* @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;
}
}
/**
* @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;
}
}
设置
/**
* @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
mComponentName = new ComponentName("cn.xy.windowmanager","cn.xy.windowmanager.MainActivity")