Android:
知识点:
-
子线程中无法更新UI,因为UI是线程不安全的。
- Android的UI单线程模式:
- 不能阻塞UI线程;
- 不能从UI线程的外部访问Android UI toolkit。
- 需要更新UI可以使用Handler传值。具体使用方式如学习总结五:
- Android的UI单线程模式:
实践项目:
一键清理内存
功能:实现Android中,结束当前活跃APP进程,释放内存的Demo。
知识点:
- ActivityManager的用法等等
public String getMemoryState(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
// Log.e(MainActivity.TAG, "剩余内存" + (info.availMem >> 100000) + "M");
Log.e(MainActivity.TAG, "是否处于低内存:" + info.lowMemory);
Log.e(MainActivity.TAG, "threshold: " + info.threshold);
memoryValueLong = (info.availMem / 1048576);
memoryValueString = Long.toString(memoryValueLong);
Log.e(MainActivity.TAG, "可用内存: " + memoryValueString + "MB"); return memoryValueString;
} public long getMemoryValueLong(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
// Log.e(MainActivity.TAG, "剩余内存" + (info.availMem >> 100000) + "M");
Log.e(MainActivity.TAG, "是否处于低内存:" + info.lowMemory);
Log.e(MainActivity.TAG, "threshold: " + info.threshold);
memoryValueLong = (info.availMem / 1048576);
return memoryValueLong;
} public void killProcess() {
ActivityManager activityManager2 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> infoList = activityManager2.getRunningAppProcesses();
List<ActivityManager.RunningServiceInfo> serviceInfoList = activityManager2.getRunningServices(200);
long beforeMem = getMemoryValueLong(MainActivity.this);
Log.d(TAG, "-----------before memory info : " + beforeMem);
int count = 0;
if (infoList != null) {
for (int i = 0; i < infoList.size(); ++i) {
ActivityManager.RunningAppProcessInfo appProcessInfo = infoList.get(i);
Log.e(TAG, "process name : " + appProcessInfo.processName);
//importance 该进程的重要程度 分为几个级别,数值越低就越重要。
Log.e(TAG, "importance : " + appProcessInfo.importance);
// 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了
// 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
if (appProcessInfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) {
String[] pkgList = appProcessInfo.pkgList;
for (int j = 0; j < pkgList.length; ++j) {
Log.e(MainActivity.TAG, "Killed package name: " + pkgList[j]);
activityManager2.killBackgroundProcesses(pkgList[j]);
count++; }
} long clearAfeterMemoryState = getMemoryValueLong(MainActivity.this);
Toast.makeText(MainActivity.this, "已清理内存:" + Long.toString(beforeMem - clearAfeterMemoryState) + " 当前内存为: " + Long.toString(clearAfeterMemoryState), Toast.LENGTH_SHORT).show();
} }
}
- 创建快捷方式:
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); }