好的,我已经了解了如何使用新的 API。我准备了一个小样本,获取有关卷存储和特定应用程序的一些信息(这次是 Play 商店,但您可以根据需要进行更改):
public class MainActivity extends AppCompatActivity {
public static final String PACKAGE_NAME = "com.android.vending";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
if (!hasUsageStatsPermission(this))
startActivityForResult(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY), 1);
else {
final Context context = this;
AsyncTask.execute(new Runnable() {
@TargetApi(VERSION_CODES.O)
@Override
public void run() {
@SuppressLint("WrongConstant") final StorageStatsManager storageStatsManager = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
final StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
final List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
final UserHandle user = android.os.Process.myUserHandle();
for (StorageVolume storageVolume : storageVolumes) {
final String uuidStr = storageVolume.getUuid();
final UUID uuid = uuidStr == null ? StorageManager.UUID_DEFAULT : UUID.fromString(uuidStr);
try {
Log.d("AppLog", "storage:" + uuid + " : " + storageVolume.getDescription(context) + " : " + storageVolume.getState());
Log.d("AppLog", "getFreeBytes:" + Formatter.formatShortFileSize(context, storageStatsManager.getFreeBytes(uuid)));
Log.d("AppLog", "getTotalBytes:" + Formatter.formatShortFileSize(context, storageStatsManager.getTotalBytes(uuid)));
Log.d("AppLog", "storage stats for app of package name:" + PACKAGE_NAME + " : ");
final StorageStats storageStats = storageStatsManager.queryStatsForPackage(uuid, PACKAGE_NAME, user);
Log.d("AppLog", "getAppBytes:" + Formatter.formatShortFileSize(context, storageStats.getAppBytes()) +
" getCacheBytes:" + Formatter.formatShortFileSize(context, storageStats.getCacheBytes()) +
" getDataBytes:" + Formatter.formatShortFileSize(context, storageStats.getDataBytes()));
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
@TargetApi(VERSION_CODES.M)
public static boolean hasUsageStatsPermission(Context context) {
//http://stackoverflow.com/a/42390614/878126
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)
return false;
AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
final int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName());
boolean granted = false;
if (mode == AppOpsManager.MODE_DEFAULT)
granted = (context.checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) == PackageManager.PERMISSION_GRANTED);
else
granted = (mode == AppOpsManager.MODE_ALLOWED);
return granted;
}
}
清单文件应该有这个:
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions"/>
编辑:请注意,UUID.fromString(uuidStr)
SD卡的部分可能会失败,谷歌的原因是这样的:
Android 无法为公共卷(SD 卡等)维护配额式统计信息。应用程序需要自己构建大小计算逻辑。
可悲的是,在 Android 11 上,由于到达“Android”文件夹的新限制,即使这也是不可能的。在这里要求改变它:
@t0m 但即使他们写了什么,官方也不是不可能按照他们所说的去做,因为存储权限在 Android 11 上受到了非常严格的限制,不允许您正确访问 Android 文件夹。要求在这里解决这个问题:
请考虑主演。