1、获取application中指定key的meta-data值
public static String getApplicationMetadata(Context context,String metaDataKey) {
ApplicationInfo info = null;
try {
PackageManager pm = context.getPackageManager();
info = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return String.valueOf(info.metaData.get(metaDataKey));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
应用场景:在程序打包上线到不同的应用市场时需要给每一个apk设置不同的渠道id,打包后查看log信息是否正确。
例如:
<meta-data
android:name="UMENG_CHANNEL"
android:value="Offline" />
调用方法:PackageUtils.getApplicationMetadata(App.getInstance()
获取到的结果就Offline
.getContext(), "UMENG_CHANNEL")
2、获取包名
public static String getPackageName(Context context){
return context.getPackageName();
}
3、获取版本名称
public static String getVersionName(Context context){
try {
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
String version = info.versionName;
return version;
} catch (Exception e) {
e.printStackTrace();
}
return "1.0";
}
4、得到版本号
public static int getVersionCode(Context context){
try {
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
int version = info.versionCode;
return version;
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
这些信息只是为了检查程序中的设置是否真确不是每次都需要的,所以通常情况下把log信息在进入应用的引导动画所在的activity中打印出来就好了。
5、验证手机号
public static boolean isMobileNo(String mobiles) {
if (mobiles == null) {
return false;
}
String patten = "^((13[0-9])|(15[^4,\\D])|(170)|(17[6-8])|(18[0-9])|(147))\\d{8}$";
Pattern p = Pattern.compile(patten);
Matcher m = p.matcher(mobiles);
return m.matches();
}
6、获取手机上的app列表
public static void getApplist(Context context) {
PackageManager manager = context.getPackageManager();
List<PackageInfo> packages = manager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (int i = 0; i < packages.size(); i++) {
PackageInfo packageInfo = packages.get(i);
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
if (packageInfo.versionName == null) {
continue;
}
String name = packageInfo.applicationInfo.loadLabel(manager).toString().trim();
Log.e("Tag", name);
}
}
}
7、dp转化为px
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
8、px转化为dp
public int pxToDp(int px) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
9、判断手机上是否装有某款app
public static boolean isAppInstalled(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> info = packageManager.getInstalledPackages(0);
List<String> pName = new ArrayList<String>();
if (info != null) {
for (int i = 0; i < info.size(); i++) {
String pn = info.get(i).packageName;
pName.add(pn);
}
}
return pName.contains(packageName);
}
10、卸载指定包名的app
public static void uninstallApp(Context context, String packageName) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent intent = new Intent(Intent.ACTION_DELETE, packageURI);
context.startActivity(intent);
}
11、获取手机的信息
public static String getPhoneInfo(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
StringBuilder sb = new StringBuilder();
sb.append("\n设备Id(IMEI) = " + tm.getDeviceId());// 15位数字的设备id
sb.append("\n手机品牌:"+android.os.Build.BRAND);//手机品牌 OnePlus Xiaomi
sb.append("\n手机型号:" + android.os.Build.MODEL);//例如GT-I9502
sb.append("\n系统名称:" + android.os.Build.DISPLAY);
sb.append("\n设备名称:" + android.os.Build.DEVICE);// m1metal--HM2014011--ja3gchnduos
sb.append("\n硬件:" + android.os.Build.HARDWARE);// 一般显示处理器型号
sb.append("\nID:" + android.os.Build.ID);// LMY47I--HM2014011--LRX22C
sb.append("\n手机7:" + android.os.Build.TAGS);// 手机一般显示release-keys,可以区分是不是模拟器
sb.append("\n用户类型:" + android.os.Build.TYPE);// user--user--user
sb.append("\n手机11:" + android.os.Build.VERSION.CODENAME);// REL
sb.append("\nandroid版本:" + android.os.Build.VERSION.RELEASE);//android版本例如 4.4.2
sb.append("\nandroidApi版本:" + android.os.Build.VERSION.SDK_INT);//手机的android APi 19
sb.append("\n手机14:" + android.os.Build.VERSION_CODES.BASE);// 1--1--1
Logger.i("info:" + sb.toString());
return sb.toString();
}
12、安装app
public static void installAPK(Context context, File file) {
if (file == null || !file.exists())
return;
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}