Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open app permission settings

Regarding granular permissions on Android M.

In case the user denies twice the request for a permission,

Or that the user mark the "Never ask again".

How can I open/link directly to the app permissions settings?

like image 480
David Avatar asked Jun 29 '15 23:06

David


People also ask

Should app permissions be on or off?

You should avoid app permissions that aren't necessary for an app to work. If the app shouldn't need access to something — like your camera or location — don't allow it. Consider your privacy when deciding whether to avoid or accept an app permission request.

Can I turn off all app permissions?

To see a more comprehensive list of permissions, you can tap on the Apps & notifications screen, then tap App permissions. In this window, you can browse apps by the permissions they access, and turn off any you like.


1 Answers

Let's assume in AndroidManifest.xml you have:

<uses-permission android:name="android.permission.WRITE_CALENDAR"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <!-- ETC. --> 

Then, as in the below sample activity, we can check for existing/granted permissions and then send the user to app settings if we're missing any permission:

package com.example.permissions.myapplication;  import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      private static final int REQUEST_APP_SETTINGS = 168;      private static final String[] requiredPermissions = new String[]{             Manifest.permission.WRITE_CALENDAR,             Manifest.permission.WRITE_CONTACTS             /* ETC.. */     };      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         /* All version with SDK_INT < 22 grant permissions on install time. */         if (Build.VERSION.SDK_INT > 22 && !hasPermissions(requiredPermissions)) {             Toast.makeText(this, "Please grant all permissions", Toast.LENGTH_LONG).show();             goToSettings();         }     }      private void goToSettings() {         Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));         myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);         myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         startActivityForResult(myAppSettings, REQUEST_APP_SETTINGS);     }      public boolean hasPermissions(@NonNull String... permissions) {         for (String permission : permissions)             if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(permission))                 return false;         return true;     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (requestCode == REQUEST_APP_SETTINGS) {             if (hasPermissions(requiredPermissions)) {                 Toast.makeText(this, "All permissions granted!", Toast.LENGTH_SHORT).show();             } else {                 Toast.makeText(this, "Permissions not granted.", Toast.LENGTH_LONG).show();             }         }         super.onActivityResult(requestCode, resultCode, data);     } } 

I just tried it on Nexus 6 / AndroidOS 6 (Marshmallow).

like image 142
Andrei Mărcuţ Avatar answered Sep 18 '22 05:09

Andrei Mărcuţ