https://github.com/1976222027/AndroidUtils
1、获取application中指定key的meta-data值
publicstaticStringgetApplicationMetadata(Context context,String metaDataKey) { ApplicationInfo info =null;try{ PackageManager pm = context.getPackageManager(); info = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);returnString.valueOf(info.metaData.get(metaDataKey)); }catch(Exception e) { e.printStackTrace(); }returnnull; }
应用场景:在程序打包上线到不同的应用市场时需要给每一个apk设置不同的渠道id,打包后查看log信息是否正确。
例如:
调用方法:PackageUtils.getApplicationMetadata(App.getInstance()
.getContext(), "UMENG_CHANNEL")
获取到的结果就Offline
publicstaticStringgetPackageName(Context context){returncontext.getPackageName(); }
publicstaticStringgetVersionName(Context context){try{ PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(),0); String version = info.versionName;returnversion; }catch(Exception e) { e.printStackTrace(); }return"1.0"; }
publicstaticintgetVersionCode(Context context){try{ PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(),0);intversion = info.versionCode;returnversion; }catch(Exception e) { e.printStackTrace(); }return1; }
publicstaticbooleanisMobileNo(String mobiles) {if(mobiles ==null) {returnfalse; } 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);returnm.matches(); }
public static void getApplist(Context context) { PackageManager manager = context.getPackageManager();List 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);} } }
publicintdpToPx(intdp) { DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();intpx = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));returnpx;}
publicintpxToDp(intpx) { DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();intdp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));returndp;}
publicstaticbooleanisAppInstalled(Context context, String packageName) {finalPackageManager packageManager = context.getPackageManager(); List info = packageManager.getInstalledPackages(0); List pName =newArrayList();if(info !=null) {for(inti =0; i < info.size(); i++) { String pn = info.get(i).packageName; pName.add(pn); } }returnpName.contains(packageName); }
publicstaticvoiduninstallApp(Context context, String packageName) { Uri packageURI = Uri.parse("package:"+ packageName); Intent intent =newIntent(Intent.ACTION_DELETE, packageURI); context.startActivity(intent); }
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位数字的设备idsb.append("\n手机品牌:"+android.os.Build.BRAND);//手机品牌 OnePlus Xiaomisb.append("\n手机型号:"+ android.os.Build.MODEL);//例如GT-I9502sb.append("\n系统名称:"+ android.os.Build.DISPLAY);sb.append("\n设备名称:"+ android.os.Build.DEVICE);// m1metal--HM2014011--ja3gchnduossb.append("\n硬件:"+ android.os.Build.HARDWARE);// 一般显示处理器型号sb.append("\nID:"+ android.os.Build.ID);// LMY47I--HM2014011--LRX22Csb.append("\n手机7:"+ android.os.Build.TAGS);// 手机一般显示release-keys,可以区分是不是模拟器sb.append("\n用户类型:"+ android.os.Build.TYPE);// user--user--usersb.append("\n手机11:"+ android.os.Build.VERSION.CODENAME);// RELsb.append("\nandroid版本:"+ android.os.Build.VERSION.RELEASE);//android版本例如 4.4.2sb.append("\nandroidApi版本:"+ android.os.Build.VERSION.SDK_INT);//手机的android APi 19sb.append("\n手机14:"+ android.os.Build.VERSION_CODES.BASE);// 1--1--1Logger.i("info:"+ sb.toString());return sb.toString();}
publicstaticvoidinstallAPK(Context context, File file) {if(file ==null|| !file.exists())return; Intent intent =newIntent(); 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); }
网友评论