1、合并map到另一个map
把一个map里面的key-value赋值给另一个map
1 2 3 4 5 6 7 8 9 10 11 12 |
Set<Map.Entry<String, String>> customOptionEntrys = masoConfig.exProperties.entrySet(); if(customOptionEntrys.size() > 0) { Iterator<Map.Entry<String, String>> oIterator = customOptionEntrys.iterator(); Map<String, String> targetMap = new HashMap<String, String>(); while (oIterator.hasNext()) { Map.Entry<String, String> oEntry = oIterator.next(); String key = oEntry.getKey(); if(!TextUtils.isEmpty(key)) { targetMap.put(key, oEntry.getValue()); } } } |
2、获取Manifest meta-data
获取AndroidManifest.xml里面meta-data标签中定义的值
1 2 3 4 5 6 7 8 9 10 11 12 |
private static String getMetaValue(Context context, String key) { String value = ""; try { ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); if (appInfo != null && appInfo.metaData != null) { value = appInfo.metaData.getString(key); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return value; } |
3、判断是否安装google play service
使用
1 |
GoogleApiAvailability.isGooglePlayServicesAvailable() |
下面的方法已经废弃:
1 |
GooglePlayServicesUtil.isGooglePlayServicesAvailable() |
使用例子:
1 2 3 4 5 |
public boolean isGooglePlayServicesAvailable(Context context){ GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context); return resultCode == ConnectionResult.SUCCESS; } |
如果google play service 检查时返回服务不可用,此时希望这个错误可修复,可以用下面的代码:
1 2 3 4 5 6 7 8 9 10 11 |
public boolean isGooglePlayServicesAvailable(Activity activity) { GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); int status = googleApiAvailability.isGooglePlayServicesAvailable(activity); if(status != ConnectionResult.SUCCESS) { if(googleApiAvailability.isUserResolvableError(status)) { googleApiAvailability.getErrorDialog(activity, status, 2404).show(); } return false; } return true; } |
4. 判断应用是否首次安装还是覆盖安装
- PackageInfo#firstInstallTime:app首次安装时间
- PackageInfo#lastUpdateTime : 安装的app最后更新时间。如果和firstInstallTime相等就代表是本地启动首次安装启动
1 2 3 4 5 6 7 8 9 10 11 |
//获取版本号(内部识别号 public static int getVersionCode(Context context) { try { Context appContext = context.getApplicationContext(); PackageInfo pi = appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0); return pi.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return 0; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * 获取app首次安装时间 * @param context * @return */ public static long getPackageFirstInstallTime(Context context) { Context appContext = context.getApplicationContext(); String name = appContext.getPackageName(); long time = 0; try { time = appContext.getPackageManager().getPackageInfo(name, 0).firstInstallTime; } catch (Exception e) { e.printStackTrace(); } return time; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * 获取当前安装包的最后更新时间 * * @param context * @return */ public static long getPackageLastUpdateTime(Context context) { Context appContext = context.getApplicationContext(); String name = appContext.getPackageName(); long time = 0; try { time = appContext.getPackageManager().getPackageInfo(name, 0).lastUpdateTime; } catch (Exception e) { e.printStackTrace(); } return time; } |
1 2 3 4 5 6 7 8 |
/** * 当次启动是否为首次安装启动 * @param context * @return */ public static boolean isFirstInstall(Context context) { return getPackageFirstInstallTime(context) == getPackageLastUpdateTime(context); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * 全新安装启动 */ public static final short APP_STARTUP_NEW_INSTALL = 1; /** * 覆盖安装启动 */ public static final short APP_STARTUP_UPGRADE = 2; /** * 普通启动类型 */ public static final short APP_STARTUP_NORMAL = 3; /** * startup flag */ private static boolean mHasStartUp = false; //default type is APP_STARTUP_NORMAL private static short mStartupType = APP_STARTUP_NORMAL; private static final Object mCheckStartupLock = new Object(); /** * 获取本次启动类型 * @param context * @return */ public static short getStartupType(Context context) { //由于是静态工具类,需要做线程安全处理 if(!mHasStartUp) { synchronized (mCheckStartupLock) { if(!mHasStartUp) { //先判断是否是首次安装启动 if(isFirstInstall(context)) { mStartupType = APP_STARTUP_NEW_INSTALL; } else { Context appContext = context.getApplicationContext(); SharedPreferences sp = appContext.getSharedPreferences("your sp name here!", Context.MODE_PRIVATE) int lastVersion = sp.getInt("last_startup_version_code", 0); int currentVersionCode = getVersionCode(appContext); if(currentVersionCode > lastVersion) { mStartupType = APP_STARTUP_UPGRADE; sp.edit().putInt("your sp name here!", currentVersionCode).apply(); } else { //if lastVersion is empty or equals with current version, it's normal startup mStartupType = APP_STARTUP_NORMAL; } } mHasStartUp = true; } } } return mStartupType; } |
5、打Jar包时打入自定义参数例如build号,和代码读取该参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
//脚本添加manifest attribute apply plugin: 'java' apply plugin: 'application' jar { baseName = 'packTool' version = '2.0.0' def buildseq = "${getDate()}" manifest { attributes( 'Main-Class': 'com.hysdk.pack.Main', 'version': version, 'buildseq': buildseq ) } } //读取manifest attribute private Attributes loadManifestValues(LogPacker logPacker) { Attributes packManifestAttr = null; Class clazz = Main.class; String className = clazz.getSimpleName() + ".class"; String classPath = clazz.getResource(className).toString(); if (!classPath.startsWith("jar")) { // Class not from JAR return packManifestAttr; } String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; try { Manifest manifest = new Manifest(new URL(manifestPath).openStream()); packManifestAttr = manifest.getMainAttributes(); } catch (MalformedURLException e) { logPacker.w(TAG, "loadManifestValues failed: "+e.getMessage()+", manifestPath:"+manifestPath); e.printStackTrace(); } catch (IOException e) { logPacker.w(TAG, "loadManifestValues failed: "+e.getMessage()+", manifestPath:"+manifestPath); e.printStackTrace(); } return packManifestAttr; } public String getManifestValue(LogPacker logPacker, String key) { if(mManifestAttributes == null) { mManifestAttributes = loadManifestValues(logPacker); } if(mManifestAttributes != null) { return mManifestAttributes.getValue(key); } else { return null; } } |
6、通过属性名读取attr内容
以读取TabLayout的attr属性为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<declare-styleable name="TabLayout"> <attr name="tabIndicatorColor" format="color" /> <attr name="tabIndicatorHeight" format="dimension" /> <attr name="tabIndicatorWidth" format="dimension" /> <attr name="tabContentStart" format="dimension" /> <attr name="tabBackground" format="reference" /> <attr name="tabMode"> <enum name="scrollable" value="0" /> <enum name="fixed" value="1" /> </attr> <attr name="tabGravity"> <enum name="fill" value="0" /> <enum name="center" value="1" /> </attr> <attr name="tabMinWidth" format="dimension" /> <attr name="tabMaxWidth" format="dimension" /> <attr name="tabTextAppearance" format="reference" /> <attr name="tabTextColor" format="color" /> <attr name="tabSelectedTextColor" format="color" /> <attr name="tabSelectedNeedBold" format="boolean"/> <attr name="tabPaddingStart" format="dimension" /> <attr name="tabPaddingTop" format="dimension" /> <attr name="tabPaddingEnd" format="dimension" /> <attr name="tabPaddingBottom" format="dimension" /> <attr name="tabPadding" format="dimension" /> <attr name="tabBottomDividerHeight" format="dimension"/> <attr name="tabBottomDividerColor" format="color"/> </declare-styleable> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public static class StyleableRHelper { private int[] mAttrs; private Context mContext; private Map<String, Integer> mResIdMap = new HashMap<String, Integer>(); public StyleableRHelper(Context context, String... attrNameArr) { mContext = context; int size = attrNameArr != null ? attrNameArr.length : 0; mAttrs = new int[size]; for(int i=0; i<size; i++) { String attrName = attrNameArr[i]; mAttrs[i] = mContext.getResources().getIdentifier(attrName, "attr", context.getPackageName()); mResIdMap.put(attrName, mAttrs[i]); } Arrays.sort(mAttrs); } public int[] getStyleAttrs() { return mAttrs; } public int getStyleIndex(String attrName) { return Arrays.binarySearch(mAttrs, mResIdMap.get(attrName)); } } RHelper.StyleableRHelper styleableRHelper = new RHelper.StyleableRHelper(context, "tabIndicatorHeight", "tabIndicatorColor", "tabBottomDividerHeight", "tabIndicatorWidth", "tabBottomDividerColor" , "tabTextAppearance", "tabPadding", "tabPaddingStart", "tabPaddingTop", "tabPaddingEnd", "tabPaddingBottom", "tabTextColor", "tabSelectedTextColor", "tabSelectedNeedBold", "tabMinWidth", "tabMaxWidth", "tabBackground", "tabContentStart", "tabMode", "tabGravity"); int[] tabLayoutAttrs = styleableRHelper.getStyleAttrs(); TypedArray a = context.obtainStyledAttributes(attrs, tabLayoutAttrs, defStyleAttr, 0); int tabIndicatorHeightIndex = styleableRHelper.getStyleIndex("tabIndicatorHeight"); this.mTabStrip.setSelectedIndicatorHeight(a.getDimensionPixelSize(tabIndicatorHeightIndex, 0)); |
思路:
- obtainStyledAttributes传入styleable attribute id的数组(styleable在R文件中的存在形式是一个attr id数组)(该数组是需要按照attr id的大小排序的),以及当前控件实例的AttributeSet对象,得到TypedArray对象
- 通过上面得到的TypedArray对象的方法传入某个attribute在styleable的index值可以得到对应资源内容