gpt4 book ai didi

android - 如何以编程方式计算已安装应用程序的所有缓存大小?

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:02 25 4
gpt4 key购买 nike

我只想显示所有已安装的应用程序名称和缓存大小的列表。缓存大小必须与 Android 设备中显示的 native 设置应用程序相同。

我能够列出所有已安装的应用程序 pkg 名称,但无法计算 native 设置应用程序显示的相同缓存大小(我说的是设置中清除缓存选项的地方)。

pkNames = getPackageManager().getInstalledPackages(0);

ArrayAdapter<PackageInfo> adapter = new ArrayAdapter<PackageInfo>(this,
android.R.layout.simple_list_item_1, pkNames);
final PackageManager pm = getPackageManager();
for(int i =0;i<pkNames.size();i++)
{
ApplicationInfo applicationInfo = pm.getApplicationInfo(pkNames.get(i).packageName.toString(), 0);
File file = new File(applicationInfo.publicSourceDir);
long size = file.length();
System.out.println("application name == "+pkNames.get(i).packageName.toString()+" ,and size of cache is == "+size(this, size));
}

我使用此代码,但它会给我该应用程序的 .apk 大小。 :(请帮忙提前致谢。

最佳答案

要直接获取已安装应用程序的缓存大小,您无法获取。使用 PackageManger我们无法直接获取有关已安装包大小的详细信息作为摘要 getPackageSizeInfo方法无法直接访问,因此使用 Java Reflection您可以调用它。如果将来方法名称更改或发生任何更改,它可能无法工作。

您需要创建 AIDL IPackageStatsObserver .aidl & PackageStats.aidl它应该在 android.content.pm 包中,因为您不能直接访问它们。

IPackageStatsObserver.aidl

package android.content.pm;
import android.content.pm.PackageStats;
oneway interface IPackageStatsObserver {
void onGetStatsCompleted(in android.content.pm.PackageStats pStats, boolean succeeded);
}

PackageStats.aidl

package android.content.pm;
parcelable PackageStats;

IPackageStatsObserver.aidl 和 PackageStats.aidl 都将其保存在 android.content.pm 包中。IDataStatus

public interface IDataStatus {
public void onStatusListner(String msg);
}

Activity

    public class MyScreen extends Activity implements OnClickListener {
public static final int FETCH_PACKAGE_SIZE_COMPLETED = 100;
public static final int ALL_PACAGE_SIZE_COMPLETED = 200;
IDataStatus onIDataStatus;
TextView lbl_cache_size;
ProgressDialog pd;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btn_get_cacheSize).setOnClickListener(this);
lbl_cache_size = (TextView) findViewById(R.id.lbl_cache_size);
// clearCache();
}

private void showProgress(String message) {
pd = new ProgressDialog(this);
pd.setIcon(R.drawable.ic_launcher);
pd.setTitle("Please Wait...");
pd.setMessage(message);
pd.setCancelable(false);
pd.show();

}

long packageSize = 0, size = 0;
AppDetails cAppDetails;
public ArrayList<PackageInfoStruct> res;

private void getpackageSize() {
cAppDetails = new AppDetails(this);
res = cAppDetails.getPackages();
if (res == null)
return;
for (int m = 0; m < res.size(); m++) {
PackageManager pm = getPackageManager();
Method getPackageSizeInfo;
try {
getPackageSizeInfo = pm.getClass().getMethod(
"getPackageSizeInfo", String.class,
IPackageStatsObserver.class);
getPackageSizeInfo.invoke(pm, res.get(m).pname,
new cachePackState());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

}
handle.sendEmptyMessage(ALL_PACAGE_SIZE_COMPLETED);
Log.v("Total Cache Size", " " + packageSize);

}

private Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FETCH_PACKAGE_SIZE_COMPLETED:
if (packageSize > 0)
size = (packageSize / 1024000);
lbl_cache_size.setText("Cache Size : " + size + " MB");
break;
case ALL_PACAGE_SIZE_COMPLETED:
if (null != pd)
if (pd.isShowing())
pd.dismiss();

break;
default:
break;
}

}

};

private class cachePackState extends IPackageStatsObserver.Stub {

@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
Log.d("Package Size", pStats.packageName + "");
Log.i("Cache Size", pStats.cacheSize + "");
Log.w("Data Size", pStats.dataSize + "");
packageSize = packageSize + pStats.cacheSize;
Log.v("Total Cache Size", " " + packageSize);
handle.sendEmptyMessage(FETCH_PACKAGE_SIZE_COMPLETED);
}

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get_cacheSize:
size = 0;
packageSize = 0;
showProgress("Calculating Cache Size..!!!");
/**
* You can also use async task
* */
new Thread(new Runnable() {

@Override
public void run() {
getpackageSize();
}
}).start();

break;
}
}

}

应用详情

public class AppDetails {
Activity mActivity;
public ArrayList<PackageInfoStruct> res = new ArrayList<PackageInfoStruct>();
public ListView list;
public String app_labels[];

public AppDetails(Activity mActivity) {
this.mActivity = mActivity;

}

public ArrayList<PackageInfoStruct> getPackages() {
ArrayList<PackageInfoStruct> apps = getInstalledApps(false); /*
* false =
* no system
* packages
*/
final int max = apps.size();
for (int i = 0; i < max; i++) {
apps.get(i);
}
return apps;
}

private ArrayList<PackageInfoStruct> getInstalledApps(boolean getSysPackages) {

List<PackageInfo> packs = mActivity.getPackageManager()
.getInstalledPackages(0);
try {
app_labels = new String[packs.size()];
} catch (Exception e) {
Toast.makeText(mActivity.getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
PackageInfoStruct newInfo = new PackageInfoStruct();
newInfo.appname = p.applicationInfo.loadLabel(
mActivity.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.datadir = p.applicationInfo.dataDir;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mActivity
.getPackageManager());
res.add(newInfo);

app_labels[i] = newInfo.appname;
}
return res;
}

class PackageInfoStruct {
String appname = "";
String pname = "";
String versionName = "";
int versionCode = 0;
Drawable icon;
String datadir = "";
}
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn_get_cacheSize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get Cache Size" />

<TextView
android:id="@+id/lbl_cache_size"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cache Size : " />

</LinearLayout>

关于android - 如何以编程方式计算已安装应用程序的所有缓存大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com