Android学习笔记_59_清除sdcard缓存

  对于手机来说,每个软件在安装时,都会在sdcard上创建一个目录,用于缓存文件。市场上针对这些软件,统一了它的sdcard上的目录,将缓存目录存放到数据库中。如果要清理,可以根据当前应用包的名称,到数据库去查询缓存路径,如果

到再进行递归删除它下的文件。

public class DemoActivity extends Activity {

    private TextView tv;

    private ProgressBar pb;

    private SQLiteDatabase db;

    private Handler handler = new Handler(){



        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            String text = (String) msg.obj;

            tv.setText(text);

        }

        

        

    };



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        tv = (TextView) this.findViewById(R.id.tv);

        pb = (ProgressBar) this.findViewById(R.id.progressBar1);



        // 判断手机内存里面是否有数据库存在

        File file = new File("/data/data/cn.itcast.clear/files/clearpath.db");

        if (!file.exists()) {

            copyfile();

        }

 // 文件写到哪里了?

            // data/data/cn.itcast.clear/files/name

        

    

        

        



        

        

        

    }



    public void start(View view) {

        db = SQLiteDatabase.openDatabase("/data/data/cn.itcast.clear/files/clearpath.db", null, SQLiteDatabase.OPEN_READONLY);

        new Thread(){

            @Override

            public void run() {

                List<PackageInfo> packinfos =    getPackageManager().getInstalledPackages(0);

                pb.setMax(packinfos.size());// 设置进度条的最大条目个数

                int total=0;

                for(PackageInfo info : packinfos){

                    String packname = info.packageName;

                    Cursor curosr = db.rawQuery("select filepath from softdetail where apkname=?", new String[]{packname});

                    if(curosr.moveToFirst()){

                        String path = curosr.getString(0);

                        System.out.println("清除"+packname+"sd卡缓存"+path);

                        File file = new File(Environment.getExternalStorageDirectory(),path);

                        deleteDir(file);

                        try {

                            sleep(500);

                        } catch (InterruptedException e) {

                            e.printStackTrace();

                        }

                    }

                    total++;

                    pb.setProgress(total);

                    curosr.close();

                    Message msg = Message.obtain();

                    msg.obj = "清除"+packname;

                    handler.sendMessage(msg);

                }

                

                Message msg = Message.obtain();

                msg.obj = "清除完毕";

                handler.sendMessage(msg);

                db.close();

            }

            

        }.start();

    }

    

    

    private void copyfile() {

        

        try {

            InputStream is = getClass().getClassLoader().getResourceAsStream(

                    "clearpath.db");

           OutputStream fos =    this.openFileOutput("clearpath.db", MODE_PRIVATE);

            byte[] buffer = new byte[1024];

            int len = 0;



            while ((len = is.read(buffer)) != -1) {

                fos.write(buffer, 0, len);

            }

            fos.flush();

            fos.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    
  //递归删除缓存文件夹下的文件
private void deleteDir(File file){ if(file.isDirectory()){ File[] files = file.listFiles(); for(int i=0;i<files.length;i++){ deleteDir(file); } }else{ file.delete(); } } }

 

你可能感兴趣的:(Android学习)