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;
效果图:
跳到系统应用信息页面清理缓存的方法:
//跳到系统应用信息页面
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);