Android获取 应用程序大小,数据大小,缓存大小

时间:2023-03-08 21:08:22

在项目中创建,android.content.pm 包名。里面创建两个aidl文件。PackageStats.aidl  和 IPackageStatsObserver.aidl。

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;

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);
}
  import android.app.Activity;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView; public class MainActivity extends Activity { private TextView tv;
private static final String ATTR_PACKAGE_STATS = "PackageStats"; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); tv = new TextView(this); setContentView(tv); getpkginfo("com.xj.notebook");
} private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
String infoString = "";
PackageStats newPs = msg.getData().getParcelable(
ATTR_PACKAGE_STATS);
if (newPs != null) {
infoString += "应用程序大小: " + formatFileSize(newPs.codeSize);
infoString += "\n数据大小: " + formatFileSize(newPs.dataSize);
infoString += "\n缓存大小: " + formatFileSize(newPs.cacheSize);
}
tv.setText(infoString);
break;
default:
break;
}
}
}; public void getpkginfo(String pkg) {
PackageManager pm = getPackageManager();
try {
Method getPackageSizeInfo = pm.getClass().getMethod(
"getPackageSizeInfo", String.class,
IPackageStatsObserver.class);
getPackageSizeInfo.invoke(pm, pkg, new PkgSizeObserver());
} catch (Exception e) {
}
} class PkgSizeObserver extends IPackageStatsObserver.Stub {
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) {
Message msg = mHandler.obtainMessage(1);
Bundle data = new Bundle();
data.putParcelable(ATTR_PACKAGE_STATS, pStats);
msg.setData(data);
mHandler.sendMessage(msg);
}
} /**
* 获取文件大小
*/
public static String formatFileSize(long length) {
String result = null;
int sub_string = 0;
if (length >= 1073741824) {
sub_string = String.valueOf((float) length / 1073741824).indexOf(
".");
result = ((float) length / 1073741824 + "000").substring(0,
sub_string + 3) + "GB";
} else if (length >= 1048576) {
sub_string = String.valueOf((float) length / 1048576).indexOf(".");
result = ((float) length / 1048576 + "000").substring(0,
sub_string + 3) + "MB";
} else if (length >= 1024) {
sub_string = String.valueOf((float) length / 1024).indexOf(".");
result = ((float) length / 1024 + "000").substring(0,
sub_string + 3) + "KB";
} else if (length < 1024)
result = Long.toString(length) + "B";
return result;
}
}
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>