枚举安装的应用并查询应用的包名和对应的APK文件路径

         //  Iterate Android packages
        List < PackageInfo >  packages  =  getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES  |
                PackageManager.GET_GIDS 
|  PackageManager.GET_CONFIGURATIONS  |
                PackageManager.GET_INSTRUMENTATION 
|  PackageManager.GET_PERMISSIONS  |
                PackageManager.GET_PROVIDERS 
|  PackageManager.GET_RECEIVERS  |
                PackageManager.GET_SERVICES 
|  PackageManager.GET_SIGNATURES);
        
for  ( int  i  =   0 ; i  <  packages.size(); i ++ ) {
            PackageInfo package_info 
=  packages.get(i);
            String package_name 
=  package_info.packageName;
            package_name 
=  package_name.trim().toLowerCase();
            Log.i(TAG, 
" Got package:  "   +  package_name);
            
try  {
                ApplicationInfo app_info 
=  getPackageManager().getApplicationInfo(package_name,
                        PackageManager.GET_META_DATA 
|  PackageManager.GET_SHARED_LIBRARY_FILES);
                Log.i(TAG, 
" Package apk file path:  "   +  app_info.sourceDir);
            } 
catch  (NameNotFoundException e) {
                e.printStackTrace();
            }
        }

 

当然,也可以硬来:

//  Iterate folder, this needs "root" privilege
File root_dir  =   new  File( " /data/app " );
File[] files 
=  root_dir.listFiles();
for  ( int  i  =   0 ; i  <  files.length; i ++ ) {
    
if  (files[i].isFile()) {
        String filename 
=  files[i].getName();
        filename 
=  filename.trim().toLowerCase();
        Log.i(TAG, 
" Got file under /data/app:  "   +  filename);

 

直接遍历/data/app或者/system/app目录。不过这就需要root权限,而且能取到的信息远远少于使用PackageManager。 

你可能感兴趣的:(apk)