获取VersionCode:
/** * 得到VersionCode * @return */ public int getVersionCode(){ String packageName = getApplicationContext().getPackageName(); try { PackageInfo packageInfo = getApplicationContext().getPackageManager().getPackageInfo(packageName,0); return packageInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return -1; }获取VersionName:
/** * 得到VersionName * @return */ public String getVersionName(){ String packageName = getApplicationContext().getPackageName(); try { PackageInfo packageInfo = getApplicationContext().getPackageManager().getPackageInfo(packageName,0); return packageInfo.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; }获取Application名称:
/** * 得到Application的名称 * @return */ public String getApplicationName(){ String packageName = getApplicationContext().getPackageName(); try { ApplicationInfo applicationInfo = getApplicationContext().getPackageManager().getApplicationInfo(packageName,0); String applicaiontName = (String) getApplicationContext().getPackageManager().getApplicationLabel(applicationInfo); return applicaiontName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; }
判断一个App是否正在运行:
/** * 判断一个App是否运行 不需要任何权限 * @return */ public boolean isRunning(){ String packageName = getApplicationContext().getPackageName(); ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses(); for(ActivityManager.RunningAppProcessInfo rapi : infos){ if(rapi.processName.equals(packageName)){ return true; } } return false; }判断一个Activity是否在前台:需要权限: android.permission.REAL_GET_TASKS (使用于 L以下,对于L以上则使用am.getRunningAppProcesses()方法)
/** * 判断一个Activity是否在前台运行 * @param activityName * @return */ public boolean isTopActivity(String activityName){ ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);//maxNum限制要获取的数量 String cmpNameTemp = null; if(runningTaskInfos != null){ cmpNameTemp = runningTaskInfos.get(0).topActivity.getShortClassName();//info.topActivity.getPackageName()得到包的名字 } if(cmpNameTemp == null){ return false; } return cmpNameTemp.endsWith(activityName); }获取应用的公钥签名:
/** * 获取应用公钥签名 * @return */ public Signature getSign(){ PackageInfo pi; Signature sign = null; try { pi = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(),PackageManager.GET_SIGNATURES); sign = pi.signatures[0]; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return sign; }比较当前签名HashCode和预设的HashCode:
/** * 比较当前签名HashCode和预设的HashCode * @param presetHashCode * @return */ public boolean signCheckWithHashCode(int presetHashCode){ Signature signature = getSign(); return signature.hashCode() == presetHashCode; }数据操作:
/** * 删除数据:cache,file,share prefs,databases */ public void clear(){ clearCache(getApplicationContext()); clearFiles(getApplicationContext()); clearSharedPreference(getApplicationContext()); clearDataBase(getApplicationContext()); } /** * 删除应用数据库目录 * @param context */ public void clearDataBase(Context context){ delete(new File("/data/data/" + context.getPackageName() + "/databases"),true); } /** * 删除应用SharedPrefrence目录 * @param context */ public void clearSharedPreference(Context context){ delete(new File("/data/data/" + context.getPackageName() + "/shared_prefs"), true); } /** * 删除应用文件目录 * @param applicationContext */ private void clearFiles(Context applicationContext) { delete(getApplicationContext().getFilesDir(),true); } /** * 删除应用缓存目录 * @param directory * @param b */ private void clearCache(Context applicationContext) { delete(getApplicationContext().getCacheDir(), true); delete(getApplicationContext().getExternalCacheDir(), true); } private void delete(File directory, boolean b) { if(directory != null && directory.exists()){ if(directory.isDirectory()){ for(File subDirectory : directory.listFiles()){ delete(subDirectory,false); } } if(!b){ directory.delete(); } } }