前言
在Android设备中,我们经常会看到与系统或者应用相关的清除功能有:清除数据、清除缓存、一键清理,这么多清除功能对于一个程序猿就够难理解了,偏偏很多安卓设备上都有这些功能,对于用户来说就更难理解,趁着在把玩手机的时候想到了这一点,索引追根究底了解他们的具体区别。
清除数据、清除缓存、一键清理的区别
清除数据
清除数据主要是清除用户配置,比如SharedPreferences、数据库等等,这些数据都是在程序运行过程中保存的用户配置信息,清除数据后,下次进入程序就和第一次进入程序时一样;
清除缓存
缓存是程序运行时的临时存储空间,它可以存放从网络下载的临时图片,从用户的角度出发清除缓存对用户并没有太大的影响,但是清除缓存后用户再次使用该APP时,由于本地缓存已经被清理,所有的数据需要重新从网络上获取,注意:为了在清除缓存的时候能够正常清除与应用相关的缓存,请将缓存文件存放在getCacheDir()或者 getExternalCacheDir()路径下。比如对微信清除缓存,则聊天记录、朋友圈缓存的用户头像、图片、文字等信息都会被清除掉,清除缓存后再次进入微信时你会发现消息记录被清空了,朋友圈的图片和用户头像需要加载一会才能正常显示。
一键清理
一键清理是系统级别的功能,它主要是杀后台进程,以达到释放内存的目的,但杀掉哪些进程和清理时设置的重要值阈值有关,重要值越大说明进程重要程度越低,如果在清理时某个进程的重要值大于该阈值,该进程就会被杀掉。比如微信等应用在后台,一件清理后会将微信和与之相关的服务都杀掉(有的服务做了特殊处理,杀不死!!!)。
参考资料
-
What’s the difference between clear cache & clear data in android settings
-
How to delete files created by the application on uninstall?
说明
为了让程序被卸载后不在文件系统中留下毫无关联的无用文件,建议将应用相关的配置和缓存文件存放在程序被卸载时会删掉的文件夹下面(音乐文件、视频文件、图片、电子书这种适合多个应用阅读和浏览的文件除外),具体路径有:
-
/data/data/package/
-
getFilesDir()
-
getCacheDir()
-
getExternalCacheDir()(是否能够在程序被卸载时被删除与API的等级有关)
-
getExternalFilesDir()(是否能够在程序被卸载时被删除与API的等级有关)
--------------------------------------------------
Android 一键清理、内存清理功能实现:
360桌面、金山清理大师等都提供了一键清理、一键加速等功能,其实就是杀一些后台进程来达到释放内存的目的。
基本思路就是列出所有运行的进程,查看其重要值(RunningAppProcessInfo.importance,值越大说明进程重要程度越低),可以设定一个阈值,
如果该进程的重要值大于该阈值,就可以杀掉该进程。
进程的重要值有以下几个等级:
[java] view plain copy
- /**
- * Constant for {@link #importance}: this is a persistent process.
- * Only used when reporting to process observers.
- * @hide
- */
- public static final int IMPORTANCE_PERSISTENT = 50;
- /**
- * Constant for {@link #importance}: this process is running the
- * foreground UI.
- */
- public static final int IMPORTANCE_FOREGROUND = 100;
- /**
- * Constant for {@link #importance}: this process is running something
- * that is actively visible to the user, though not in the immediate
- * foreground.
- */
- public static final int IMPORTANCE_VISIBLE = 200;
- /**
- * Constant for {@link #importance}: this process is running something
- * that is considered to be actively perceptible to the user. An
- * example would be an application performing background music playback.
- */
- public static final int IMPORTANCE_PERCEPTIBLE = 130;
- /**
- * Constant for {@link #importance}: this process is running an
- * application that can not save its state, and thus can't be killed
- * while in the background.
- * @hide
- */
- public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
- /**
- * Constant for {@link #importance}: this process is contains services
- * that should remain running.
- */
- public static final int IMPORTANCE_SERVICE = 300;
- /**
- * Constant for {@link #importance}: this process process contains
- * background code that is expendable.
- */
- public static final int IMPORTANCE_BACKGROUND = 400;
- /**
- * Constant for {@link #importance}: this process is empty of any
- * actively running code.
- */
- public static final int IMPORTANCE_EMPTY = 500;
直接上代码:
[java] view plain copy
- package com.android.clearmemory;
- import android.app.Activity;
- import android.app.ActivityManager;
- import android.app.ActivityManager.MemoryInfo;
- import android.app.ActivityManager.RunningAppProcessInfo;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import java.util.List;
- public class ClearMemoryActivity extends Activity {
- private static final String TAG = "ClearMemoryActivity";
- /**
- * Called when the activity is first created.
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button clear = (Button) findViewById(R.id.clear);
- clear.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //To change body of implemented methods use File | Settings | File Templates.
- ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
- List<ActivityManager.RunningServiceInfo> serviceInfos = am.getRunningServices(100);
- long beforeMem = getAvailMemory(ClearMemoryActivity.this);
- Log.d(TAG, "-----------before memory info : " + beforeMem);
- int count = 0;
- if (infoList != null) {
- for (int i = 0; i < infoList.size(); ++i) {
- RunningAppProcessInfo appProcessInfo = infoList.get(i);
- Log.d(TAG, "process name : " + appProcessInfo.processName);
- //importance 该进程的重要程度 分为几个级别,数值越低就越重要。
- Log.d(TAG, "importance : " + appProcessInfo.importance);
- // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了
- // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
- if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
- String[] pkgList = appProcessInfo.pkgList;
- for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名
- Log.d(TAG, "It will be killed, package name : " + pkgList[j]);
- am.killBackgroundProcesses(pkgList[j]);
- count++;
- }
- }
- }
- }
- long afterMem = getAvailMemory(ClearMemoryActivity.this);
- Log.d(TAG, "----------- after memory info : " + afterMem);
- Toast.makeText(ClearMemoryActivity.this, "clear " + count + " process, "
- + (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show();
- }
- });
- ClearMemoryFloatView.instance(getApplicationContext()).createView();
- }
- //获取可用内存大小
- private long getAvailMemory(Context context) {
- // 获取android当前可用内存大小
- ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- MemoryInfo mi = new MemoryInfo();
- am.getMemoryInfo(mi);
- //mi.availMem; 当前系统的可用内存
- //return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
- Log.d(TAG, "可用内存---->>>" + mi.availMem / (1024 * 1024));
- return mi.availMem / (1024 * 1024);
- }
- }
注意:
我这里选择阈值是IMPORTANCE_VISIBLE级别的,也就是非可见的后台进程和服务会被杀掉(一些系统进程肯定除外)。
清理的效果跟金山清理大师和360桌面的一键清理效果差不多。
如果不想杀的太凶,可以选择IMPORTANCE_SERVICE级别,杀掉那些长时间没用或者空进程了,
这个级别的清理力度不够大,达不到金山清理大师的效果。