每日学习总结<二> 2015-9-1
Android:
知识点:
- 子线程中无法更新UI,因为UI是线程不安全的。
- Android的UI单线程模式:
- 不能阻塞UI线程;
- 不能从UI线程的外部访问Android UI toolkit。
- 需要更新UI可以使用Handler传值。具体使用方式如学习总结五:
- Android的UI单线程模式:
实践项目:
一键清理内存
功能:实现Android中,结束当前活跃APP进程,释放内存的Demo。
知识点:
- ActivityManager的用法等等
1 public String getMemoryState(Context context) { 2 3 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 4 ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); 5 activityManager.getMemoryInfo(info); 6 // Log.e(MainActivity.TAG, "剩余内存" + (info.availMem >> 100000) + "M"); 7 Log.e(MainActivity.TAG, "是否处于低内存:" + info.lowMemory); 8 Log.e(MainActivity.TAG, "threshold: " + info.threshold); 9 memoryValueLong = (info.availMem / 1048576); 10 memoryValueString = Long.toString(memoryValueLong); 11 Log.e(MainActivity.TAG, "可用内存: " + memoryValueString + "MB"); 12 13 return memoryValueString; 14 } 15 16 public long getMemoryValueLong(Context context) { 17 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 18 ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); 19 activityManager.getMemoryInfo(info); 20 // Log.e(MainActivity.TAG, "剩余内存" + (info.availMem >> 100000) + "M"); 21 Log.e(MainActivity.TAG, "是否处于低内存:" + info.lowMemory); 22 Log.e(MainActivity.TAG, "threshold: " + info.threshold); 23 memoryValueLong = (info.availMem / 1048576); 24 return memoryValueLong; 25 } 26 27 public void killProcess() { 28 ActivityManager activityManager2 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 29 List<ActivityManager.RunningAppProcessInfo> infoList = activityManager2.getRunningAppProcesses(); 30 List<ActivityManager.RunningServiceInfo> serviceInfoList = activityManager2.getRunningServices(200); 31 long beforeMem = getMemoryValueLong(MainActivity.this); 32 Log.d(TAG, "-----------before memory info : " + beforeMem); 33 int count = 0; 34 if (infoList != null) { 35 for (int i = 0; i < infoList.size(); ++i) { 36 ActivityManager.RunningAppProcessInfo appProcessInfo = infoList.get(i); 37 Log.e(TAG, "process name : " + appProcessInfo.processName); 38 //importance 该进程的重要程度 分为几个级别,数值越低就越重要。 39 Log.e(TAG, "importance : " + appProcessInfo.importance); 40 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了 41 // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着 42 if (appProcessInfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) { 43 String[] pkgList = appProcessInfo.pkgList; 44 for (int j = 0; j < pkgList.length; ++j) { 45 Log.e(MainActivity.TAG, "Killed package name: " + pkgList[j]); 46 activityManager2.killBackgroundProcesses(pkgList[j]); 47 count++; 48 49 } 50 } 51 52 long clearAfeterMemoryState = getMemoryValueLong(MainActivity.this); 53 Toast.makeText(MainActivity.this, "已清理内存:" + Long.toString(beforeMem - clearAfeterMemoryState) + " 当前内存为: " + Long.toString(clearAfeterMemoryState), Toast.LENGTH_SHORT).show(); 54 } 55 56 57 } 58 }
- 创建快捷方式:
private void addShortCut() { Log.e(MainActivity.TAG, "====addShortCut()===="); Intent shortCutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式名称 shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); shortCutIntent.putExtra("duplicate", false); ComponentName componentName = new ComponentName(this.getPackageName(), "." + this.getLocalClassName()); shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, new Intent(Intent.ACTION_MAIN).setComponent(componentName)); //快捷方式的图标 Intent.ShortcutIconResource iconResource = new Intent.ShortcutIconResource().fromContext(this, R.drawable.ic_launcher); shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); sendBroadcast(shortCutIntent); }