アプリケーションのアプリケーション サイズ (codeSize) を返すプログラムを作成しようとしています。これにはSDKを通過する必要があることがわかりました。私はそうしました。ただし、コードをテストしようとすると、サイズとして 0 が返されます。さまざまなアプローチを試みましたが、値は変わりませんでした。ここに私が書いたコードがあります
package android.content.pm;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.RemoteException;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
class AppInfo{
private String appName = "";
private String packageName = "";
private String versionName = "";
private int versionCode = 0;
private long appSize = 0;
private long dataSize = 0;
private long cacheSize = 0;
private String appPermission = "";
private String associateProcess = "";
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
private List<AppInfo> retrievedInstallApps() throws NoSuchMethodException {
List<AppInfo> applicationInfo = new ArrayList<AppInfo>();
PackageManager pm = getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
for (int i = 0; i<packages.size(); i++) {
PackageInfo p = packages.get(i);
if (!isSystemPackage(p)) {
AppInfo newApplicationInfo = new AppInfo();
newApplicationInfo.appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
newApplicationInfo.packageName = p.packageName;
newApplicationInfo.versionName = p.versionName;
newApplicationInfo.versionCode = p.versionCode;
newApplicationInfo.appPermission = p.applicationInfo.permission;
newApplicationInfo.associateProcess = p.applicationInfo.processName;
//get the application size
//-------------------------------------
Method getPackageSizeInfo = pm.getClass().getMethod(
"getPackageSizeInfo", String.class, IPackageStatsObserver.class);
try {
getPackageSizeInfo.invoke(pm, "com.android.mms", new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
Log.i("", "appSize: " + pStats.codeSize);
//newApplicationInfo.appSize = pStats.codeSize;
//newApplicationInfo.cacheSize = pStats.cacheSize;
//newApplicationInfo.dataSize = pStats.dataSize;
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
applicationInfo.add(newApplicationInfo);
}
}
return applicationInfo;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<AppInfo> newAppInfo = null;
try {
newAppInfo = retrievedInstallApps();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < newAppInfo.size(); i++) {
AppInfo app = newAppInfo.get(i);
/*Log.i("", "appName:\t " + app.appName + " \t\t " +
"packageName:\t " + app.packageName + " \t\t " +
"versionName:\t " + app.versionName + " \t\t " +
"versionCode:\t " + String.valueOf(app.versionCode) + " \t\t " +
"appPermission:\t " + String.valueOf(app.appPermission) + " \t\t " +
"associateProcess:\t " + app.associateProcess + " \t\t " +
"appSize:\t " + String.valueOf(app.appSize) + " \t\t " +
"cacheSize:\t " + String.valueOf(app.cacheSize) + " \t\t " +
"dataSize:\t " + String.valueOf(app.dataSize) + " \n ");*/
}
}
}