如何在android中获取已安装app的列表

时间:2016-04-04 07:39:47

标签: android

我想获得安装在Android设备中的应用程序列表及其个别应用程序的大小和RAM使用情况。请帮助我。给我任何参考

2 个答案:

答案 0 :(得分:1)

只需尝试此代码即可获取已安装应用的列表:

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

答案 1 :(得分:0)

获取应用程序的RAM大小:

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");
    }
}

获取安装应用程序大小,包括缓存大小,代码大小和数据大小:

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);
    }
}