简体   繁体   中英

How to get list of installed app with size in android

我想获取安装在 android 设备中的应用程序列表,包括单个应用程序的大小和 RAM 使用情况。请帮助我。给我任何参考

Try this code ONLY to get list of installed apps:

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

Get app's RAM size:

private void getRunningAppProcessInfo() {
    mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

    List<runningappprocessinfo> runningAppProcessesList = mActivityManager.getRunningAppProcesses();

    for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcessesList) {
        int pid = runningAppProcessInfo.pid;
        int uid = runningAppProcessInfo.uid;
        String processName = runningAppProcessInfo.processName;

        int[] pids = new int[] {pid};
        Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(pids);
        int memorySize = memoryInfo[0].dalvikPrivateDirty;

        Log.d(TAG, "processName="+processName+",pid="+pid+",uid="+uid+",memorySize="+memorySize+"kb");
    }
}

Get install app size, include cache size, code size and data size:

public void queryPacakgeSize(Context context, String pkgName) {
    PackageManager pm = context.getPackageManager(); 
    try {
        Class<?> clz = pm.getClass();
        if (Build.VERSION.SDK_INT > 16) {
            Method myUserId= UserHandle.class.getDeclaredMethod("myUserId");
            int userID = (Integer) myUserId.invoke(pm);
            Method getPackageSizeInfo = clz.getDeclaredMethod(
                "getPackageSizeInfo", String.class, int.class, IPackageStatsObserver.class);
            getPackageSizeInfo.invoke(pm, pkgName, userID, new PkgSizeObserver());
        } else {
            Method getPackageSizeInfo = clz.getDeclaredMethod(
                "getPackageSizeInfo", String.class, IPackageStatsObserver.class);
            getPackageSizeInfo.invoke(pm, pkgName, new PkgSizeObserver());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } 
}

private class PkgSizeObserver extends IPackageStatsObserver.Stub {
    /***
     * @param pStatus
     * @param succeeded
     */
    @Override
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
    throws RemoteException {
        long cachesize = pStats.cacheSize;
        long datasize = pStats.dataSize; 
        long codesize = pStats.codeSize; 
        long totalsize = cachesize + datasize + codesize;
        Log.i(TAG, "cachesize--->" + cachesize + " datasize---->"
                    + datasize + " codeSize---->" + codesize);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM