149 lines
5.6 KiB
Java
149 lines
5.6 KiB
Java
package com.ttstd.dialer.utils;
|
|
|
|
import android.content.ActivityNotFoundException;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.ActivityInfo;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ResolveInfo;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.net.Uri;
|
|
import android.text.TextUtils;
|
|
|
|
import com.kongzue.dialogx.dialogs.PopTip;
|
|
import com.ttstd.dialer.R;
|
|
|
|
import java.util.List;
|
|
|
|
public class ApkUtils {
|
|
private static final String TAG = "ApkUtils";
|
|
|
|
public static boolean isInstalled(Context context, String packageName) {
|
|
if (TextUtils.isEmpty(packageName)) return false;
|
|
try {
|
|
return context.getPackageManager().getPackageInfo(packageName, 0) != null;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 判断是否为系统应用
|
|
*
|
|
* @param context 上下文
|
|
* @param pkgName 包名
|
|
* @return
|
|
*/
|
|
public static boolean isSystemApp(Context context, String pkgName) {
|
|
try {
|
|
PackageManager pm = context.getPackageManager();
|
|
PackageInfo pi = pm.getPackageInfo(pkgName, 0);
|
|
return (pi.applicationInfo.flags & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
Logger.e("isSystemApp: ", "NameNotFoundException:" + e.getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static boolean openPackage(Context context, String packageName) {
|
|
if (TextUtils.isEmpty(packageName)) {
|
|
return false;
|
|
}
|
|
if (!isInstalled(context, packageName)) {
|
|
PopTip.show("应用未安装").iconError();
|
|
return false;
|
|
}
|
|
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
|
|
if (intent != null) {
|
|
try {
|
|
context.startActivity(intent);
|
|
return true;
|
|
} catch (ActivityNotFoundException e) {
|
|
Logger.e(TAG, "openPackage: " + e.getMessage());
|
|
}
|
|
}
|
|
Logger.e(TAG, "openPackage: can not open " + packageName);
|
|
PopTip.show("打开失败").iconError();
|
|
return false;
|
|
}
|
|
|
|
public static boolean openApp(Context context, ComponentName componentName) {
|
|
Intent intent = new Intent()
|
|
.setComponent(componentName)
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
try {
|
|
context.startActivity(intent);
|
|
return true;
|
|
} catch (ActivityNotFoundException e) {
|
|
Logger.e(TAG, "openApp: " + e.getMessage());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static Drawable getDrawableFromComponentName(Context context, ComponentName componentName) {
|
|
try {
|
|
PackageManager packageManager = context.getPackageManager();
|
|
ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, 0);
|
|
ApplicationInfo applicationInfo = activityInfo.applicationInfo;
|
|
Drawable activityIcon = applicationInfo.loadIcon(packageManager);
|
|
return activityIcon;
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
// 处理未找到组件的情况
|
|
e.printStackTrace();
|
|
// 可以根据需要设置一个默认图标或提示用户
|
|
}
|
|
return context.getDrawable(R.mipmap.ic_launcher);
|
|
}
|
|
|
|
public static String getAppName(Context context, ComponentName componentName) {
|
|
try {
|
|
PackageManager packageManager = context.getPackageManager();
|
|
ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, 0);
|
|
ApplicationInfo applicationInfo = activityInfo.applicationInfo;
|
|
String name = applicationInfo.loadLabel(packageManager).toString();
|
|
return name;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return "未知";
|
|
}
|
|
|
|
public static List<ResolveInfo> getAllLauncherResolveInfo(Context context) {
|
|
PackageManager packageManager = context.getPackageManager();
|
|
Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
// 查询所有包含CATEGORY_LAUNCHER的Activity
|
|
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL);
|
|
return resolveInfoList;
|
|
}
|
|
|
|
public static List<ResolveInfo> getResolveInfoByPackageName(Context context, String packageName) {
|
|
PackageManager packageManager = context.getPackageManager(); // 在Activity中使用
|
|
|
|
// 构建查询Intent
|
|
Intent intent = new Intent(Intent.ACTION_MAIN, null);
|
|
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
intent.setPackage(packageName); // 设置包名过滤
|
|
|
|
// 查询匹配的Activity列表
|
|
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
|
|
return resolveInfoList;
|
|
}
|
|
|
|
|
|
public static void openAppStore(Context context, String pkg) {
|
|
Uri uri = Uri.parse("market://details?id=" + pkg);
|
|
Intent storeIntent = new Intent(Intent.ACTION_VIEW, uri);
|
|
storeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
try {
|
|
context.startActivity(storeIntent);
|
|
} catch (Exception e1) {
|
|
Logger.e(TAG, "openWeixin storeIntent: " + e1.getMessage());
|
|
}
|
|
}
|
|
}
|