获得应用的存储信息
private void getAppStorageInfo(String packageName){
StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
List<StorageVolume> storageVolumes = storageManager.getStorageVolumes(); //app可能安装在外部存储上面,外部存储有一个唯一的UUID做区分
UserHandle user = android.os.Process.myUserHandle();
Log.d("zwt", ":::"+storageVolumes.size());
try {
for (StorageVolume storageVolume : storageVolumes) {
final String uuidStr = storageVolume.getUuid();
Log.d("zwt", "uuidStr:::"+uuidStr);
final UUID uuid = uuidStr == null ? StorageManager.UUID_DEFAULT : UUID.fromString(uuidStr);
StorageStats storageStats = storageStatsManager.queryStatsForPackage(uuid, packageName, user);
//应用程序的大小。这包括 APK 文件、优化的编译器输出和解压的原生库
long appBytes = storageStats.getAppBytes();
//应用程序缓存数据的大小。这包括存储在 Context.getCacheDir() 和 Context.getCodeCacheDir() 下的文件
long cacheBytes = storageStats.getCacheBytes();
//应用程序所有数据的大小。这包括存储在 Context.getDataDir()、Context.getCacheDir()、Context.getCodeCacheDir() 下的文件
long dataBytes = storageStats.getDataBytes();
//主外部共享存储中所有缓存数据的大小。这包括存储在 Context.getExternalCacheDir() 下的文件
long externalCacheBytes = storageStats.getExternalCacheBytes();
}
}catch (PackageManager.NameNotFoundException | IOException e) {
e.printStackTrace();
}
return ;
}
需要声明权限
<!-- 获得app缓存大小 -->
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
清除应用数据缓存
在公版设置中的位置
清除数据:\packages\apps\Settings\src\com\android\tv\settings\device\apps\ClearDataPreference.java
清除缓存:\packages\apps\Settings\src\com\android\tv\settings\device\apps\ClearCachePreference.java
位置:\packages\apps\\Settings\src\com\android\tv\settings\device\apps\AppManagementFragment.java
主要调用方法,这两个方法都是不能被第三方应用调用的方法,必须是系统级的应用,且有系统的签名,第三方只能用反射的方法调用
am.clearApplicationUserData(packageName, new ClearUserDataObserver());
pm.deleteApplicationCacheFiles(packageName, new ClearUserDataObserver());
这两个方法要传入一个aidl服务,来回调通知数据是否清除成功
1.导入aidl
位置: frameworks/base/core/java/android/content/pm/IPackageDataObserver.aidl
将此aidl文件复制到项目aidl文件夹下,注意包名保持一致
aidl说明:https://blog.csdn.net/weixin_44128558/article/details/112154294
2.实现
// 清除缓存
public void clearCache(String packageName){
if (context!=null){
PackageManager pm = context.getPackageManager();
pm.deleteApplicationCacheFiles(packageName, new ClearUserDataObserver());
}else {
Log.d(TAG, "clearCache() context is null");
}
}
// 清除数据
public void clearData(String packageName){
if (context!=null){
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
boolean success = am.clearApplicationUserData(packageName, new ClearUserDataObserver());
}else {
Log.d(TAG, "clearData() context is null");
}
}
//清除回调
static class ClearUserDataObserver extends IPackageDataObserver.Stub {
private final String TAG = "zwt:ClearUserDataObserver";
@Override
public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
if (succeeded){
Log.d(TAG, "清理成功");
}else {
Log.d(TAG, "清理失败");
}
}
}
// 卸载apk
public void deleteApk(String packageName) {
if (context == null){
return ;
}
Uri uri = Uri.fromParts("package", packageName, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
轻松搞定