遍歷系統中apk信息

1、獲取系統中所有apk信息

獲取所有應用信息,以及apk名字、apk路徑

private void getInstalledApks() {
        //獲取所有應用信息
        List<ApplicationInfo> installedAllAppList = mPm.getInstalledApplications(0);
        List<String> installThirdAppList = new ArrayList<String>();
        List<ApplicationInfo> systemAppInfoList= new ArrayList<ApplicationInfo>();
        for (ApplicationInfo appInfo : installedAllAppList) {
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0 && (appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
                //把第三方應用信息加入列表                
                installThirdAppList.add(appInfo.packageName);
            }
            else{
                //把系統應用信息保存在列表中
                systemAppInfoList.add(appInfo); 
            }
            //通過此方法,獲取apk的名字,即manifest中“android:label”的值
            Log.i(TAG, "the apk softName is "+appInfo.loadLabel(mPm).toString());
            Log.i(TAG, "the apk path is "+appInfo.sourceDir);
        }
    }

2、獲取未安裝的apk

@SuppressLint("NewApi")
    private boolean getUninstallApks(List<String> apkNameList){

        apksNameList.clear();
        Uri uri = MediaStore.Files.getContentUri("external");
        String selection = MediaStore.Files.FileColumns.MIME_TYPE + " =='"+
                MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk")+"'";
        Log.d(TAG,"uri = "+uri+", selection = "+selection);
        Cursor cursor = null;

        try{
            cursor = mContext.getContentResolver().query(uri,new String[]{MediaStore.Files.FileColumns.DATA},selection,null,null);
            Log.d(TAG,"cursor = "+cursor+" ,count = "+cursor.getCount());
            if(cursor!=null){
                while(cursor.moveToNext()){
                    Log.d(TAG,"added to list, path = "+cursor.getString(0));
                    apksNameList.add(cursor.getString(0));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG,e.getMessage());
        }finally {
            if(cursor != null){
                cursor.close();
            }
        }
        return true;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章