後台清理

360桌面、金山清理大師等都提供了一鍵清理、一鍵加速等功能,其實就是殺一些後台進程來達到釋放內存的目的。

基本思路就是列出所有運行的進程,查看其重要值(RunningAppProcessInfo.importance,值越大說明進程重要程度越低),可以設定一個閾值,

如果該進程的重要值大於該閾值,就可以殺掉該進程。

進程的重要值有以下幾個等級:

/**
* 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;

直接上代碼:
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);
}

}


作者:依然綠茶
來源:CSDN
原文:https://blog.csdn.net/whu_zhangmin/article/details/19123283
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

用指令開啟或關閉 SD 卡合併到內存

撰寫說明…

今天老婆說剛買沒多久的手機又滿了,不能裝東西,想說颱風天來幫她合併 SD 卡內存,這樣她又更能盡情裝 APP 了(?),結果系統一直顯示無法合併 (因為速度太慢),但是其實測速以後還好阿,因此用手動的方式來設定合併內存。

這篇有用的對象如下

  1. Android 6.0 以上設備
  2. 會開啟 adb 跟 debug 模式
  3. 不管 SD 卡的速度 (因為合併內存一定會降速度,很差的卡降更多)
  4. 合併內存功能被鎖起來,或是因為速度偵測而無法合併的都可以使用

合併內存

  1. 備份 SD 卡
  2. adb start-server
  3. adb shell sm list-volumes all [應該類似 public:179,32 mounted … 這樣,後面的 179,32 等會會用到]
  4. adb shell sm set-force-adoptable true
  5. adb shell sm partition disk:179,32 private [這步將會把 SD 卡合併到內存,包含格式化,等了很久才完成,這時候看手機應該看不到 SD 卡了]
  6. adb shell sm set-force-adoptable false
  7. adb shell sm list-volumes all [應該會變成 private:179,32 mounted … 這樣,原來的 public 不見了]
  8. 重開機
  9. 檢查一下,到內存那邊移轉資料,還原 SD 卡資料,完成

恢復成可攜式 SD 卡

  1. 備份 SD 卡
  2. adb start-server
  3. adb shell sm list-volumes all [看 ID,179,32]
  4. adb shell sm set-force-adoptable true
  5. adb shell sm partition disk:179,32 public [這步會把 SD 設為可攜式 SD 卡,包含格式化]
  6. adb shell sm set-force-adoptable false
  7. adb shell sm list-volumes all [應該會變到 public 裡]
  8. 重開機
  9. 檢查一下,還原 SD 卡資料,完成