0

I need to check runtime-permissions for the background application whom I am sending some data through Intent.

I am broadcasting an Intent from appA to App-B in the background. Now, based on data in Intent app-B will start performing some task in the background itself. Hence, by the time I explicitly open appB, it will be ready having some data needed for further operation.

Now, I need to check run-time(Internet) permission for appB. is there any way to achieve the scenario?

003
  • 197
  • 1
  • 12
  • why don't you use [PackageManager.checkPermission(String permName, String pkgName)](https://developer.android.com/reference/android/content/pm/PackageManager#checkPermission(java.lang.String,%20java.lang.String))? – Bö macht Blau Feb 13 '19 at 16:19
  • This answer gives you all installed packages that have the permission: https://stackoverflow.com/a/30131420/949224 – dr_g Feb 13 '19 at 16:29
  • @0X0nosugar@dr_g Your answers are useful. I have question though. Can I grant the permission from appA, if the desired package doesn't have the permission granted? – 003 Feb 13 '19 at 17:35
  • Sorry, I can't imagine this is possible for normal apps. Permissions have to be granted by the user. Else they wouldn't give more security to users, so why bother at all? – Bö macht Blau Feb 13 '19 at 17:40
  • @0X0nosugar I understand your concern. but the application I am working is a child app of appA, where user perform initial operations on appA and appB doest rest of the operation going forward. and for that as I said, we are broadcasting some Intent with data. Hence, I have encounter this situation. – 003 Feb 13 '19 at 19:13
  • On the other hand, for the INTERNET permission, you only have to fill in the uses-permisison tag in the Manifest. Since it is considered "normal" (as opposed to "dangerous"), you don't have to ask for it explicitly nor can the users ever revoke it. So it's more a matter of developing/ testing than a matter of permission handling at runtime – Bö macht Blau Feb 13 '19 at 19:20

1 Answers1

0
if(hasPermission("com.appb.packge",new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}){
    //your intent
 }
boolean hasPermission(String pkgName,String permissions[]) {
    PackageManager packageManager = getPackageManager();
    boolean hasPermission = false;
    for(String permission:permissions) {
        if (packageManager.checkPermission(permission, pkgName) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
        hasPermission = true;
    }
    return hasPermission;
}
Mudassar
  • 1,566
  • 4
  • 21
  • 31