5

I used this Topic

I try this code but did not work :

PACKAGE_NAME = context.getApplicationContext().getPackageName();

try {
  pi = context.getPackageManager().getPackageInfo(PACKAGE_NAME, PackageManager.GET_PERMISSIONS);
  for (String perm : pi.requestedPermissions) {
    Log.e("Foo", perm);
  }
} catch (Exception e) {
}

But it could not help me. I have the application list, I want to get the permission that used on each of them. How can I handle it?

UPDATE: like the photo, When clicking on "دسترسی ها", I want to get the permission that used in that app.(for example in a telegram: Internet, storage, call, camera,...)

enter image description here

UPDATE 2:

I will share the adapter code for my problem

My Adapter:

class AppViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {



    AppViewHolder(View itemView, Context context, List<App> apps) {
      super(itemView);
      txt_show_permission = itemView.findViewById(R.id.txt_show_permission);



      /*The String Buffer For Permissions*/
      appNameAndPermissions = new StringBuffer();
      PackageManager pm = context.getPackageManager();
      List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
      for (ApplicationInfo applicationInfo : packages) {
        Log.d(TAG, "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);

        PackageInfo packageInfo = null;
        try {
          packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
          appNameAndPermissions.append(packageInfo.packageName + "*******:\n");


          //Get Permissions
          requestedPermissions = packageInfo.requestedPermissions;

          if (requestedPermissions != null) {
            for (int i = 0; i < requestedPermissions.length; i++) {
              Log.d(TAG, requestedPermissions[i]);
              appNameAndPermissions.append(requestedPermissions[i] + "\n");
            }

            appNameAndPermissions.append("\n");
          }

        } catch (PackageManager.NameNotFoundException e) {
          e.printStackTrace();
        }


      }
    }

set On Click Listener On txt_show_permission in onBindViewHolder:

holder.txt_show_permission.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        showDialog(String.valueOf(appNameAndPermissions));

      }
    });

Method for dialog in adapter class:

 public void showDialog(String txtPermission) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.show_permission_dialog);

    TextView txt_permission = dialog.findViewById(R.id.txt_permission);
    Button btn_ok = dialog.findViewById(R.id.btn_ok);
    txt_permission.setText(txtPermission);

    btn_ok.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.dismiss();
      }
    });
    dialog.show();

  }
sana ebadi
  • 4,318
  • 29
  • 36
  • 1
    It's not really clear from your question what the problem is. Getting a list of all permissions an app has listed in its manifest [is easy](https://stackoverflow.com/a/34811179/1524450). So is the problem that you can't get a list of all installed apps? (_"I can get The App Package Name"_ seems to contradict that). Also, please write your question in a reasonable manner; don't capitalize words randomly (it's Quite Distracting when Trying To Read the Text). – Michael Feb 28 '19 at 20:42
  • Ok, I edit it...I want to Get The Permission For Each app I click On IT – sana ebadi Feb 28 '19 at 20:52
  • You want to be able to get the permissions for each app that you click on, right? So what is the problem you are facing with the solution in your link? Also please post whatever code you have written – Kunj Mehta Mar 01 '19 at 05:44
  • @KunjMehta No, I want When Click On Each App, I will Get The Permission That Used In This App.I Write The Code On That Link But It Did Not Work for me – sana ebadi Mar 01 '19 at 08:44
  • please, see the update question. I added the photo – sana ebadi Mar 01 '19 at 08:52
  • For your code to work you will need an app having name com.example.foo. For example, to get permissions of Telegram you will need to write org.telegram.me there instead and you will have to write this for each app – Kunj Mehta Mar 01 '19 at 09:15
  • Yes, I get This But I can not use it. How can Use It? I want to give me a String. Please see the update – sana ebadi Mar 01 '19 at 09:36

1 Answers1

2
  1. You can loop through all the app names and get their permissions and store them in a String Buffer like this: https://stackoverflow.com/a/14672557/10058326

  2. Or since you want permissions to be shown on button click, you can add for each app the code you have tried with the proper app name in a OnButtonClickListener

  3. Or you can extract the relevant permissions from the StringBuffer made earlier each time the button is clicked

EDIT: See these links on how to create a OnItemClickListener for the Recycler View. You can get the position of the row that was clicked and through that get the app name in that row which you can pass to another function. Then write code inside that function to get permissions for the app name passed and display it https://antonioleiva.com/recyclerview-listener/

https://hackernoon.com/android-recyclerview-onitemclicklistener-getadapterposition-a-better-way-3c789baab4db

https://gist.github.com/riyazMuhammad/1c7b1f9fa3065aa5a46f

EDIT 2: Instead of passing appNameAndPermissions to showDialog which contains the whole list, you need to extract permissions of a certain app from the String Buffer. Here's how:

     String app_name = itemView.findViewById(R.id.app_name_text_view).getText().toString();
int indexOfApp = appNameAndPermissions.indexOf(app_name);
int indexOfLastPermission = appNameAndPermissions.indexOf("\n", indexOfApp);
String permissions = appNameAndPermissions.substring(indexOfApp, indexOfLastPermission);
Kunj Mehta
  • 224
  • 2
  • 10
  • thanks, I used this link but did not work for me. can u explain for me? that permission `````` is deprected! – sana ebadi Mar 01 '19 at 09:59
  • 1
    What is the error you are getting? I tested it and the StringBuffer part works fine for me even without the GET_TASKS permission. You need help converting to a String? – Kunj Mehta Mar 01 '19 at 10:33
  • I have a Problem. This code gets all The Permission of all Application in the phone. I want When to click On Special app, just show this permission. so help me tnx – sana ebadi Mar 01 '19 at 11:27
  • 1
    How have you implemented the above layout? Have you used a ListView or any other View or you have kept adding buttons and images one by one? I will share the code accordingly – Kunj Mehta Mar 01 '19 at 18:03
  • Did u saw the photo in my post?I have RecyclerView like the photo , I want when click on textView that is "بررسی" for each app , the permission for that app show me in dialog or toast , no problem in this just I want the app permission as i said. – sana ebadi Mar 02 '19 at 06:58
  • 1
    See the edit. Search for itemClickListeners for RecyclerView on Github or net and you will get many implementations. Use any. You won't need a StringBuffer this way – Kunj Mehta Mar 02 '19 at 17:49
  • No Dear, I know Click On View In Recycler View Adapter. see the code I will send. In OnBindViewHolder I write it and work like charm but I need something else – sana ebadi Mar 02 '19 at 17:52
  • ```holder.txt_show_permission.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(String.valueOf(requestedPermissions[0])); } });``` like it . I want Get the The permission that use in that application I cliked – sana ebadi Mar 02 '19 at 17:54
  • 1
    First, I am assuming txt_show_permission is your button. Second, you will have to make sure you setOnClickListener in onBindViewHolder and not where you create an instance of the adapter. Third, you will have to loop through the requestedPermissions array. Fourth, I would appreciate you sharing whole of the relevant code so that I can make out the context, flow and variable names – Kunj Mehta Mar 02 '19 at 18:02
  • Thank u, please see my update. I share the adapter code for my question and problem. thanks for help dear – sana ebadi Mar 02 '19 at 18:10
  • Is there any error you are getting? Seems fine to me. Is the requestedPermissions accessible inside the Listener? – Kunj Mehta Mar 02 '19 at 18:13
  • No, I have not any error. but I get The All Permission For All Applications That In list. I want to get the permission that uses in the app that I click on it.do u understand me? – sana ebadi Mar 02 '19 at 18:16
  • Thanks , What is this : String app_name = itemView.findViewById(R.id.app_name_text_view).the textView that contains the app name ? – sana ebadi Mar 02 '19 at 18:36
  • ```holder.txt_show_permission.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String app_name =holder.txt_name.getText().toString(); int indexOfApp = appNameAndPermissions.indexOf(app_name); int indexOfLastPermission = appNameAndPermissions.indexOf("\n", indexOfApp); String permissions = appNameAndPermissions.substring(indexOfApp, indexOfLastPermission); showDialog(permissions); } });``` I get crash! – sana ebadi Mar 02 '19 at 18:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189326/discussion-between-kunj-mehta-and-sana-ebadi). – Kunj Mehta Mar 02 '19 at 18:40