首先看下我们常见的Manifest配置图:
通过上面的划分,我们可以拆分出PackageInfo、ApplicationInfo、ActivityInfo、ServiceInfo、ProviderInfo几个方面,对常用的应用基本信息进行说明。
一、PackageInfo
1.1 获取 packageInfo
public static PackageManager getPackageManager(@NonNull Context context) {
return context.getPackageManager();
}
public static PackageInfo getPackageInfo(@NonNull Context context, int flag) {
try {
return getPackageManager(context).getPackageInfo(context.getPackageName(), flag);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
1.2 一些常用方法
1.2.1 获取包名
/**
* 获取包名
*/
public static synchronized String getPackageName(@NonNull Context context) {
return context.getPackageName();
}
1.2.2 获取首次安装时间、获取最后一次安装时间
public static Long getFirstInstallTime(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, 0);
if (packageInfo != null) {
return packageInfo.firstInstallTime;
} else {
return null;
}
}
public static Long getLastUpdateTime(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, 0);
if (packageInfo != null) {
return packageInfo.lastUpdateTime;
} else {
return null;
}
}
1.2.3 获取权限
/**
* 获取自定义权限 permission
*
* @param context
* @return
*/
public static PermissionInfo[] getPermissions(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_PERMISSIONS);
if (packageInfo != null) {
return packageInfo.permissions;
} else {
return null;
}
}
/**
* 获取uses-permission 列表
*
* @param context
* @return
*/
public static String[] getRequestedPermissions(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_PERMISSIONS);
if (packageInfo != null) {
return packageInfo.requestedPermissions;
} else {
return null;
}
}
/**
* 获取 uses-permission 权限是否获得状态
*
* @param context
* @return
*/
public static int[] getRequestedPermissionsFlags(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_PERMISSIONS);
if (packageInfo != null) {
return packageInfo.requestedPermissionsFlags;
} else {
return null;
}
}
1.2.4 获取 versionCode、versionName
/**
* @param context
* @return
*/
public static Long getVersionCode(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, 0);
if (packageInfo != null) {
return packageInfo.getLongVersionCode();
} else {
return null;
}
}
public static String getVersionName(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, 0);
if (packageInfo != null) {
return packageInfo.versionName;
} else {
return null;
}
}
1.2.5 获取签名信息
/**
* 获取签名信息,Android 9.0 以前使用
*
* @param context
* @return
*/
public static Signature[] getSignatures1(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_SIGNATURES);
if (packageInfo != null) {
return packageInfo.signatures;
}
return null;
}
/**
* 获取签名信息,Android 9.0 以后使用
*
* @param context
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.P)
public static SigningInfo getSignatures2(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_SIGNING_CERTIFICATES);
if (packageInfo != null) {
return packageInfo.signingInfo;
}
return null;
}
1.2.6 获取Android 4 大组件
public static ActivityInfo[] getActivities(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_ACTIVITIES);
if (packageInfo != null) {
return packageInfo.activities;
} else {
return null;
}
}
public static ServiceInfo[] getServices(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_SERVICES);
if (packageInfo != null) {
return packageInfo.services;
} else {
return null;
}
}
public static ActivityInfo[] getReceivers(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_RECEIVERS);
if (packageInfo != null) {
return packageInfo.receivers;
} else {
return null;
}
}
public static ProviderInfo[] getProviders(@NonNull Context context) {
PackageInfo packageInfo = getPackageInfo(context, PackageManager.GET_PROVIDERS);
if (packageInfo != null) {
return packageInfo.providers;
} else {
return null;
}
}
1.2.7 获取已安装的 app 列表
/**
* 获取已安装的 app列表
* @param context
* @return
*/
public static List<PackageInfo> getInstlledPackages(@NonNull Context context) {
int uninstalled = android.os.Build.VERSION.SDK_INT >= 24 ?
PackageManager.MATCH_UNINSTALLED_PACKAGES : PackageManager.GET_UNINSTALLED_PACKAGES;
return getPackageManager(context).getInstalledPackages(uninstalled);
}
二、ApplicationInfo
1.1 获取 ApplicationInfo
public static ApplicationInfo getApplicationInfo(@NonNull Context context) {
return context.getApplicationInfo();
}
1.2 一些常用方法
1.2.1 获取程序名称、icon 以及基本描述信息
/**
* 获取应用程序名称
*/
public static synchronized String getAppName(@NonNull Context context) {
ApplicationInfo appInfo;
try {
appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String appName = appInfo.loadLabel(context.getPackageManager()) + "";
return appName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取应用图标 bitmap
*
* @param context
*/
public static synchronized Bitmap getAppIcon(@NonNull Context context) {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = context.getApplicationContext()
.getPackageManager();
applicationInfo = packageManager.getApplicationInfo(
context.getPackageName(), 0);
Drawable d = packageManager.getApplicationIcon(applicationInfo); //xxx根据自己的情况获取drawable
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();
return bm;
} catch (PackageManager.NameNotFoundException e) {
}
return null;
}
public static int getDescreptionRes(@NonNull Context context) {
ApplicationInfo info = getApplicationInfo(context);
if (info != null) {
return info.descriptionRes;
}
return 0;
}
1.2.2 获取 minSdkVersion、TargetSdkVersion
@RequiresApi(api = Build.VERSION_CODES.N)
public static int getMinSdkVersion(@NonNull Context context) {
ApplicationInfo info = getApplicationInfo(context);
if (info != null) {
return info.minSdkVersion;
}
return 0;
}
public static int getTargetSdkVersion(@NonNull Context context) {
ApplicationInfo info = getApplicationInfo(context);
if (info != null) {
return info.targetSdkVersion;
}
return 0;
}
1.2.3 获取 application 的一些状态、例如判断是否是系统应用
/**
* 获取application的一些状态
*
* @param context
* @return
*/
public static int getFlags(@NonNull Context context) {
ApplicationInfo info = getApplicationInfo(context);
if (info != null) {
return info.flags;
}
return 0;
}
public static boolean isSystemApp(@NonNull Context context) {
int flags = getFlags(context);
return (flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM;
}
三、ActivityInfo、ServiceInfo、ProviderInfo
3.1 获取
context.getPackageManager().getActivityInfo
context.getPackageManager().getServiceInfo
context.getPackageManager().getProviderInfo
3.2 常用方法
可以在https://developer.android.google.cn/直接查询。
四、设备信息
4.1 常用方法
4.1.1 获取手机型号、产品型号、厂商、Android 版本
/**
* 获取手机型号
* <p>
* android.os.Build提供以下信息:
* String BOARD The name of the underlying board, like "goldfish".
* String BRAND The brand (e.g., carrier) the software is customized for, if any.
* String DEVICE The name of the industrial design.
* String FINGERPRINT A string that uniquely identifies this build.
* String HOST
* String ID Either a changelist number, or a label like "M4-rc20".
* String MODEL The end-user-visible name for the end product.
* String PRODUCT The name of the overall product.
* String TAGS Comma-separated tags describing the build, like "unsigned,debug".
* long TIME
* String TYPE The type of build, like "user" or "eng".
* String USER
*/
public String getPhoneModel() {
return android.os.Build.MODEL;
}
/**
* 整体产品
*
* @return
*/
public String getProduct() {
return android.os.Build.PRODUCT;
}
/**
* 厂商
*
* @return
*/
public static String getManufacturer() {
return Build.MANUFACTURER;
}
/**
* Android 版本
*
* @return
*/
public static String getAndroidVersion() {
return Build.VERSION.RELEASE;
}
4.1.2 获取 IMSI、IMEI、SIM卡号
单卡:
/**
* 获取移动用户标志,IMSI
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
*/
@SuppressLint("HardwareIds")
public static String getIMSI(@NonNull Context ctx) {
String strResult = null;
TelephonyManager telephonyManager = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getSubscriberId;permission deny");
return null;
}
strResult = telephonyManager.getSubscriberId();
}
return strResult;
}
/**
* 获取设备ID\IMEI
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
*/
@SuppressLint("HardwareIds")
public static String getIMEI(@NonNull Context context) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getImei;permission deny");
return null;
}
TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
return telephonyMgr != null ? telephonyMgr.getDeviceId() : null;
}
/**
* 获取SIM卡号
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
*/
@SuppressLint("HardwareIds")
public static String getSIM(@NonNull Context ctx) {
String strResult = null;
TelephonyManager telephonyManager = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getSim;permission deny");
return null;
}
strResult = telephonyManager.getSimSerialNumber();
}
return strResult;
}
多卡:
/**
* 获取移动用户标志,IMSI(卡2)
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
*/
public static String getIMSIBySoltId(@NonNull Context context, int soltId) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getIMSIById;permission deny");
return null;
}
Object object = getPhoneInfo(context, getSubIdBySoltId(context, soltId), "getSubscriberId");
if (object != null) {
return String.valueOf(object);
}
return null;
}
/**
* 获取设备ID\IMEI(卡2)
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
*/
public static String getIMEIBySoltId(@NonNull Context context, int soltId) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getIMEIById;permission deny");
return null;
}
Object object = getPhoneInfo(context, getSubIdBySoltId(context, soltId), "getDeviceId");
if (object != null) {
return String.valueOf(object);
}
return null;
}
/**
* 获取SIM卡号(卡2)
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
*/
public static String getSIMBySoltId(@NonNull Context context, int soltId) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getSIMById;permission deny");
return null;
}
Object object = getPhoneInfo(context, getSubIdBySoltId(context, soltId), "getSimSerialNumber");
if (object != null) {
return String.valueOf(object);
}
return null;
}
public static int getSubIdBySoltId(@NonNull Context context, int slotId) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "DeviceUtil;getSubId;permission deny");
return -1;
}
SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(
Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if (mSubscriptionManager != null) {
SubscriptionInfo info = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(slotId);
if (info != null) {
return info.getSubscriptionId();
}
}
return -1;
}
private static Class[] getMethodParamTypes(@NonNull String methodName) {
Class[] params = null;
try {
Method[] methods = TelephonyManager.class.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methodName.equals(methods[i].getName())) {
params = methods[i].getParameterTypes();
if (params.length >= 1) {
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return params;
}
private static Object getPhoneInfo(@NonNull Context context, int subId, @NonNull String methodName) {
Object value = null;
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) {
Method method = tm.getClass().getMethod(methodName, getMethodParamTypes(methodName));
if (subId >= 0) {
value = method.invoke(tm, subId);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
4.1.3 获取基站信息
/**
* 获取基站信息
* <p>
* 用到的权限:
* <uses-permission android:name="android.permission.READ_PHONE_STATE" />
* <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
*/
@SuppressLint("HardwareIds")
public static SCell getCellInfo(@NonNull Context ctx) {
SCell cell = new SCell();
TelephonyManager tm = null;
try {
tm = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
} catch (Exception e) {
return null;
}
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return null;
}
String IMSI = tm != null ? tm.getSubscriberId() : null;
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
if (IMSI != null) {
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
cell.NETWORK_TYPE = "CHINA MOBILE";
GsmCellLocation location = (GsmCellLocation) tm
.getCellLocation();
if (location == null) {
cell = null;
} else {
String operator = tm.getNetworkOperator();
if (operator.length() > 4) {
int mcc = Integer.parseInt(operator.substring(0, 3));
int mnc = Integer.parseInt(operator.substring(3));
int cid = location.getCid();
int lac = location.getLac();
cell.MCC = mcc;
cell.MNC = mnc;
cell.LAC = lac;
cell.CID = cid;
} else {
cell = null;
}
}
} else if (IMSI.startsWith("46001")) {
cell.NETWORK_TYPE = "CHINA UNICOM";
GsmCellLocation location = (GsmCellLocation) tm
.getCellLocation();
if (location == null) {
cell = null;
} else {
String operator = tm.getNetworkOperator();
if (operator.length() > 4) {
int mcc = Integer.parseInt(operator.substring(0, 3));
int mnc = Integer.parseInt(operator.substring(3));
int cid = location.getCid();
int lac = location.getLac();
cell.MCC = mcc;
cell.MNC = mnc;
cell.LAC = lac;
cell.CID = cid;
} else {
cell = null;
}
}
} else if (IMSI.startsWith("46003")) {
cell.NETWORK_TYPE = "CHINA TELECOM";
CdmaCellLocation location = (CdmaCellLocation) tm
.getCellLocation();
if (location == null) {
cell = null;
} else {
String operator = tm.getNetworkOperator();
if (operator.length() > 4) {
int mcc = Integer.parseInt(operator.substring(0, 3));
int mnc = Integer.parseInt(operator.substring(3));
int cid = location.getBaseStationId();
int lac = location.getNetworkId();
cell.MCC = mcc;
cell.MNC = mnc;
cell.LAC = lac;
cell.CID = cid;
} else {
cell = null;
}
}
} else {
cell = null;
}
} else {
cell = null;
}
return cell;
}
/**
* 基站信息
*/
public static class SCell {
public String NETWORK_TYPE;
public int MCC;
public int MNC;
public int LAC;
public int CID;
public JSONObject toJSON() {
JSONObject json = new JSONObject();
try {
json.put("network_type", NETWORK_TYPE);
json.put("mcc", MCC);
json.put("MNC", MNC);
json.put("LAC", LAC);
json.put("CID", CID);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
@Override
public String toString() {
return "SCell{" +
"NETWORK_TYPE='" + NETWORK_TYPE + '\'' +
", MCC=" + MCC +
", MNC=" + MNC +
", LAC=" + LAC +
", CID=" + CID +
'}';
}
}
4.1.4 获取屏幕真实高度、宽度、密度、状态栏高度
/**
* 获取屏幕宽度
*
* @param context
* @return
*/
public static int getRealScreenWidth(@NonNull Context context) {
DisplayMetrics outMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
wm.getDefaultDisplay().getRealMetrics(outMetrics);
}
return outMetrics.widthPixels;
}
/**
* 获取实际屏幕高度
*
* @param context
* @return
*/
public static int getRealScreenHeight(@NonNull Context context) {
DisplayMetrics outMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
wm.getDefaultDisplay().getRealMetrics(outMetrics);
}
return outMetrics.heightPixels;
}
/**
* 获取密度
*
* @param context
* @return
*/
public static float getScreenDensity(@NonNull Context context) {
DisplayMetrics outMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
wm.getDefaultDisplay().getRealMetrics(outMetrics);
}
return outMetrics.density;
}
/**
* 获取状态栏高度
*
* @param context
* @return
*/
public static int getStatusBarHeight(@NonNull Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}
五 参考
https://blog.csdn.net/hanfengzqh/article/details/71480503
示例:https://github.com/TonyChen-9102/ApplicationUtil