android缓存清理demo,手机APP缓存的获取和清理功能的实现

package com.loaderman.appcachedemo;

import android.content.pm.IPackageDataObserver;

import android.content.pm.IPackageStatsObserver;

import android.content.pm.PackageManager;

import android.content.pm.PackageStats;

import android.os.Bundle;

import android.os.RemoteException;

import android.support.v7.app.AppCompatActivity;

import android.text.format.Formatter;

import android.view.View;

import android.widget.Button;

import java.io.File;

import java.io.FileOutputStream;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

private PackageManager mPM;

private Button btnCache;

private Button btnClean;

private long cacheSize;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//模拟缓存

moniCache();

btnCache = (Button) findViewById(R.id.btn_cache);

btnClean = (Button) findViewById(R.id.btn_clean);

mPM = getPackageManager();

}

private void moniCache() {

//获取当前缓存路径: data/data/包名/cache

File cacheDir = getCacheDir();

//往缓存里面写点东西模拟

File cacheFile = new File(cacheDir, "cache.txt");

try {

FileOutputStream out = new FileOutputStream(cacheFile);

out.write("jfaklsdjfaklsdjfklasdjfkladsfjlkasdjflkasdflkasdjf".getBytes());

out.flush();

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public void startScan(View view) {

SaoMiaoCache();

}

private void SaoMiaoCache() {

try {

Method method = mPM.getClass().getMethod("getPackageSizeInfo",

String.class, IPackageStatsObserver.class);

method.invoke(mPM, getPackageName(), new MyObserver());

} catch (Exception e) {

e.printStackTrace();

}

}

public void cleanCache(View view){

try {

Method method = mPM.getClass().getMethod

("freeStorageAndNotify", long.class, IPackageDataObserver.class);

method.invoke(mPM, Long.MAX_VALUE, new IPackageDataObserver.Stub() {

//子线程

@Override

public void onRemoveCompleted(String packageName, boolean succeeded)

throws RemoteException {

runOnUiThread(new Runnable() {

@Override

public void run() {

SaoMiaoCache();

}

});

}

});

} catch (Exception e) {

e.printStackTrace();

}

}

class MyObserver extends IPackageStatsObserver.Stub {

//在子线程运行

@Override

public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws

RemoteException {

//缓存大小

cacheSize = pStats.cacheSize;

System.out.println(cacheSize);

runOnUiThread(new Runnable() {

@Override

public void run() {

btnCache.setText("缓存大小:" + Formatter.formatFileSize(getApplicationContext(), cacheSize));

}

});

}

}

}

activity_main.xml

添加权限:

在main下面创建aidl文件

新建包名为:android.content.pm

IPackageDataObserver.aidl

/*

**

** Copyright 2007, The Android Open Source Project

**

** Licensed under the Apache License, Version 2.0 (the "License");

** you may not use this file except in compliance with the License.

** You may obtain a copy of the License at

**

** http://www.apache.org/licenses/LICENSE-2.0

**

** Unless required by applicable law or agreed to in writing, software

** distributed under the License is distributed on an "AS IS" BASIS,

** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

** See the License for the specific language governing permissions and

** limitations under the License.

*/

package android.content.pm;

/**

* API for package data change related callbacks from the Package Manager.

* Some usage scenarios include deletion of cache directory, generate

* statistics related to code, data, cache usage(TODO)

* {@hide}

*/

oneway interface IPackageDataObserver {

void onRemoveCompleted(in String packageName, boolean succeeded);

}

IPackageStatsObserver.aidl

/*

**

** Copyright 2007, The Android Open Source Project

**

** Licensed under the Apache License, Version 2.0 (the "License");

** you may not use this file except in compliance with the License.

** You may obtain a copy of the License at

**

** http://www.apache.org/licenses/LICENSE-2.0

**

** Unless required by applicable law or agreed to in writing, software

** distributed under the License is distributed on an "AS IS" BASIS,

** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

** See the License for the specific language governing permissions and

** limitations under the License.

*/

package android.content.pm;

import android.content.pm.PackageStats;

/**

* API for package data change related callbacks from the Package Manager.

* Some usage scenarios include deletion of cache directory, generate

* statistics related to code, data, cache usage(TODO)

* {@hide}

*/

oneway interface IPackageStatsObserver {

void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);

}

PackageStats.aidl

/* //device/java/android/android/view/WindowManager.aidl

**

** Copyright 2007, The Android Open Source Project

**

** Licensed under the Apache License, Version 2.0 (the "License");

** you may not use this file except in compliance with the License.

** You may obtain a copy of the License at

**

** http://www.apache.org/licenses/LICENSE-2.0

**

** Unless required by applicable law or agreed to in writing, software

** distributed under the License is distributed on an "AS IS" BASIS,

** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

** See the License for the specific language governing permissions and

** limitations under the License.

*/

package android.content.pm;

parcelable PackageStats;

效果图:

fe993846c83df9ae556242e73d8aa149.png

跳到系统应用信息页面清理缓存的方法:

//跳到系统应用信息页面

Intent infoIntent = new Intent();

infoIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

Uri uri = Uri.fromParts("package", getPackageName(), null);//Uri.parse

// ("package:" + mCurrentInfo.packageName);

infoIntent.setData(uri);

startActivity(infoIntent);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值