[mobilesafe] 04_清理缓存界面

Android 4.0

<?xml version="1.0" encoding="utf-8"?>
<!-- 37、 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        style="@style/wizard_textview_title"
        android:gravity="center"
        android:text="清 理 缓 存 " />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <ProgressBar
            android:id="@+id/pb_clean_cache"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="30dip" />
        <TextView
            android:id="@+id/tv_clean_status"
            android:layout_width="match_parent"
            android:layout_height="30dip"
            android:gravity="center_vertical"
            android:text="一键清理缓存"
            android:textColor="#ffffffff"
            android:textSize="20sp" />
    </RelativeLayout>
    <Button
        android:id="@+id/btn_clean_cache"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:background="@drawable/btn_green_selector"
        android:text="一键清理缓存" />
</LinearLayout> 2、 CleanCacheActivity.java 
package cn.zengfansheng.mobilesafe;
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
/**
 * 23、清理缓存Activity
 * @author hacket
 */
public class CleanCacheActivity extends Activity {
    private final String TAG = "CleanCacheActivity";
    private ProgressBar pb_clean_cache;// 清理的进度条
    private Button btn_clean_cache;// 清理的按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_clean_cache);
        pb_clean_cache = (ProgressBar) this.findViewById(R.id.pb_clean_cache);
        btn_clean_cache = (Button) this.findViewById(R.id.btn_clean_cache);
        
        btn_clean_cache.setOnClickListener(new OnClickListener() {
            
            int count = 0;
            @Override
            public void onClick(View v) {
                
                // 由于获取缓存是一个很耗时的操作,所以要放在子线程中进行
                new Thread() {
                    public void run() {
                        Log.i(TAG"开始清理缓存~~~");
                        PackageManager packageManager = getPackageManager();
                        List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
                        pb_clean_cache.setMax(installedPackages.size());
                        for (PackageInfo packageInfo : installedPackages) {
                            count++;
                            pb_clean_cache.setProgress(count);
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
            }
        });
    }
}
结果: