Android获取App应⽤、缓存、数据等⼤⼩适配8.0(仿存储
空间)
image
⽬录
⼀、前⾔
⼆、效果预览
三、实现步骤
(1)开启权限
(2)AIDL⽂件导⼊
(3)引⽤⼯具类
(4)使⽤⽅式
四、功能分解
(1)单例模式
(2)设置监听数据获取回调
(3)开始获取数据
五、Demo地址
六、参考⽂档
七、内容推荐
⼀、前⾔
最近仿存储空间做了个demo,其中最⿇烦的地⽅就是获取APP应⽤⼤⼩了。做完之后⾃⼰总结把获取⽅式封装成了⼯具,⽅便以后再次使⽤。期望⼤家喜欢。如果感觉还不错的话,不妨顶⼀个,收藏⼀下...。如果有什么不妥的地⽅也可以留⾔指出,相互学习,共同进步
⼆、效果预览
先看看Demo、不满意请使劲怼作者、不要怼我哈
image
三、实现步骤
1. 开启权限
2. AIDL⽂件导⼊
3. 引⽤⼯具类
4. 使⽤⽅式
image (3)引⽤⼯具类
AppSizeUtils.class
import android.app.usage.StorageStats;
import android.app.usage.StorageStatsManager;
t.Context;
t.pm.IPackageStatsObserver;
t.pm.PackageManager;
t.pm.PackageStats;
import android.os.Build;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.support.annotation.RequiresApi;
import java.io.IOException;
import flect.Method;
import java.util.List;
import java.util.UUID;
/
**
* TODO 获取APP应⽤缓存⼤⼩数据⼤⼩应⽤⼤⼩
*/
public class AppSizeUtils {
private static AppSizeUtils mApiUrl;
private AppSizeUtils() { }
public static AppSizeUtils getInstance() {
if (mApiUrl == null) {
synchronized (AppSizeUtils.class) {
if (mApiUrl == null) {
mApiUrl = new AppSizeUtils();
}
}
}
return mApiUrl;
}8.0怎么更新
/**
* TODO 获取应⽤总⼤⼩
*/
public void init(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getAppSizeO(context);
} else {
} else {
getAppsize(context);
}
}
/**
* 获取应⽤的⼤⼩
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public void getAppSizeO(Context context) {
StorageStatsManager storageStatsManager = (StorageStatsManager) SystemService(Co
ntext.STORAGE_STATS_SERVICE);        StorageManager storageManager = (StorageManager) SystemService(Context.STORAGE_SERVICE);
//获取所有应⽤的StorageVolume列表
List<StorageVolume> storageVolumes = StorageVolumes();
for (StorageVolume item : storageVolumes) {
String uuidStr = Uuid();
UUID uuid;
if (uuidStr == null) {
uuid = StorageManager.UUID_DEFAULT;
} else {
uuid = UUID.fromString(uuidStr);
}
int uid = getUid(context, PackageName());
//通过包名获取uid
StorageStats storageStats = null;
try {
storageStats = storageStatsManager.queryStatsForUid(uuid, uid);
} catch (IOException e) {
e.printStackTrace();
}
if(onBackListent!=null){
onBackListent.CacheBytes(), DataBytes(), AppBytes());
}
}
}
/**
* 根据应⽤包名获取对应uid
*/
public int getUid(Context context, String pakName) {
try {
PackageManager().getApplicationInfo(pakName, PackageManager.GET_META_DATA).uid;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
/**
* 获取应⽤⼤⼩8.0以下
*/
public void getAppsize(Context context) {
try {
Method method = Method("getPackageSizeInfo", new Class[]{String.class, IPackageStatsObserver.class});            // 调⽤ getPackageSizeInfo ⽅法,需要两个参数:1、需要检测的应⽤包名;2、回调
method.PackageManager(), PackageName(), new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) {
if(onBackListent!=null){
onBackListent.backData(pStats.cacheSize, pStats.dataSize, deSize);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public OnBackListent onBackListent;
public interface OnBackListent {
void backData(long cacheSize, long dataSize, long codeSize);
}