Android获取系统应用及安装应用的权限列表
博客专区 > LiSteven 的博客 > 博客详情
Android获取系统应用及安装应用的权限列表
LiSteven 发表于5年前
Android获取系统应用及安装应用的权限列表
  • 发表于 5年前
  • 阅读 4950
  • 收藏 17
  • 点赞 1
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

主要通过PackageManager中的方法

代码:

public class InstallApps extends ListActivity {
   

    HashMap<String, String[]> map=new HashMap<String, String[]>();
    List<String> packagelist=new ArrayList<String>();
    List<String> systemApp=new ArrayList<String>();
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PackageManager pm=getPackageManager();
      //List<PackageInfo> mPackageinfo=  pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
       Intent query=new Intent(Intent.ACTION_MAIN);
       query.addCategory("android.intent.category.LAUNCHER");
        List<ResolveInfo> resolves= pm.queryIntentActivities(query, PackageManager.GET_ACTIVITIES);
       for (int i = 0; i < resolves.size(); i++) {
          ResolveInfo info= resolves.get(i);

  / /判断是否为系统级应用
         if((info.activityInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0){
             /*安装的应用*/
             String packagename=info.loadLabel(pm).toString();    
            String[] permission;
            try {
                permission = pm.getPackageInfo(info.activityInfo.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;//获取权限列表
                packagelist.add(packagename);
                map.put(packagename, permission);
          } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
          } else{
              /*系统应用*/
              String packagename=info.loadLabel(pm).toString();
              String[] permission;
            try {
                permission = pm.getPackageInfo(info.activityInfo.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;//获取权限列表
                systemApp.add(packagename);
                map.put(packagename, permission);
             } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
             }
        
             
          }
       
    }
    /*  判断是否属于系统应用
      for (int i = 0; i < mPackageinfo.size(); i++) {
       PackageInfo info=mPackageinfo.get(i);
       if((info.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0){
           String packagename=info.applicationInfo.loadLabel(pm).toString();    
          String[] permission= info.requestedPermissions;
          packagelist.add(packagename);
          map.put(packagename, permission);
        } else{
            String packagename=info.applicationInfo.loadLabel(pm).toString();
            String[] permission= info.requestedPermissions;
             systemApp.add(packagename);
            map.put(packagename, permission);
           
        }
    }*/
      packagelist.addAll(systemApp);
   
      getListView().setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, packagelist));
      getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            // TODO Auto-generated method stub
            String[]permission=map.get(packagelist.get(position));
            StringBuilder sb=new StringBuilder();
            for (int i = 0; i < permission.length; i++) {
              sb.append("权限"+permission[i]+"\n");
            }
            Toast.makeText(getApplicationContext(), sb,Toast.LENGTH_LONG ).show();
           
        }
       });
       
       
    };

}

共有 人打赏支持
LiSteven
粉丝 39
博文 214
码字总数 11616
×
LiSteven
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: