Permission from Manifest Doesn't Work in Android 6

android manifest permission don't work

Your testing device is api 23 and above i.e. marshmallow and above. In this case you need to request runtime permission. Please go through below link

https://developer.android.com/training/permissions/requesting.html

Permission from manifest doesn't work in Android 6

Thanks to CommonsWare's blog post, I got some clue.

Assuming your code is in Activity or Fragment, check the overlay permission and make a request for it if necessary:

public static int OVERLAY_PERMISSION_REQ_CODE = 1234;

public void someMethod() {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
}

Then, re-check the permission for better UX:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
}
}
}

Cannot resolve Manifest.permission.ACCESS_FINE_LOCATION

For the first part, you should be using <uses-permission> according the the Android Devlopers site. Try making sure you declare your permissions directly under the <manifest> tag, not in your <application> tag. It's hard to know what your problem is without seeing your entire manifest file. Check out the link I posted above for more info on how to declare permissions in your manifest.

In regards to your runtime permissions problem:

With uses-permissions Cannot resolve that..

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

Why?

Make sure you're using android.Manifest instead of my.app.package.Manifest. A lot of times Android Studio will default to the latter instead of the former.

So, your new line of code would look like:

new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};

Edit: I reformatted my answer.

Edit 2: Be wary of importing android.Manifest. It can cause issues if you're also importing my.app.package.Manifest. Other than that import android.Manifest is another valid way to resolve this issue.

Android permissions - Manifest or android.manifest

When you are asking for if permission is allowed you should use android.Manifest.permission.XX and then for asking you should use Manifest.permission.XX
Here is an example:

 public static boolean checkCameraPermissions(Activity activity, int permission) {
if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CAMERA}, permission);
return false;
}
return true;
}

Android permission doesn't work even if I have declared it

(the following is extracted from a blog post of mine about this)

The big reason for not getting your permission nowadays is because
your project has a targetSdkVersion of 23 or higher, and the permission
that you are requesting is "dangerous". In Android 6.0, this includes:

  • ACCEPT_HANDOVER
  • ACCESS_BACKGROUND_LOCATION
  • ACCESS_MEDIA_LOCATION
  • ACTIVITY_RECOGNITION
  • ANSWER_PHONE_CALLS
  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION
  • ADD_VOICEMAIL
  • BODY_SENSORS
  • CALL_PHONE
  • CAMERA
  • GET_ACCOUNTS
  • PROCESS_OUTGOING_CALLS
  • READ_CALENDAR
  • READ_CALL_LOG
  • READ_CELL_BROADCASTS
  • READ_CONTACTS
  • READ_EXTERNAL_STORAGE
  • READ_PHONE_STATE
  • READ_SMS
  • RECEIVE_MMS
  • RECEIVE_SMS
  • RECEIVE_WAP_PUSH
  • RECORD_AUDIO
  • SEND_SMS
  • USE_SIP
  • WRITE_CALENDAR
  • WRITE_CALL_LOG
  • WRITE_CONTACTS
  • WRITE_EXTERNAL_STORAGE

For these permissions, not only does your targetSdkVersion 23+ app
need to have the <uses-permission> element(s), but you also have
to ask for those permissions at runtime from the user on Android 6.0+
devices, using methods like checkSelfPermission() and
requestPermissions().

As a temporary workaround, drop your targetSdkVersion below 23.

However, eventually, you will have some reason to want your
targetSdkVersion to be 23 or higher. At that time, you will need
to adjust your app to use the new runtime permission system.
The Android documentation has
a page dedicated to this topic.

Permission Dialog not Showing Up Android 6.0.1 (Marshmallow)

If you have taken some DANGEROUS PERMISSION in your application which is crucial for user’s perspective that may cause privacy issue like (SEND SMS, READ CONTACTS) then in marshmallow only taking permission in the MANIFEST will never be enough. You have to take user’s permission in the run time(in JAVA) only when the application is going to execute the specific feature.

Application permission in run time will only work in android OS 6 (Marshmallow) and above.

Add Permission to Manifest.java

<uses-permission android:name="android.permission.SEND_SMS" />

Try my code

private static final int PERMISSION_REQUEST = 100;

//this is the onclick listener of send button
public void send(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
Snackbar.make(findViewById(R.id.rl), "You need to grant SEND SMS permission to send sms",
Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
}
}).show();
} else {
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
}
} else {
sendSMS();
}
} else {
sendSMS();
}
}

private void sendSMS() {
String phoneNumber = "1234";
String msg="hello, this is a text message";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, msg, null, null);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

Snackbar.make(findViewById(R.id.rl), "Permission Granted",
Snackbar.LENGTH_LONG).show();
sendSMS();

} else {

Snackbar.make(findViewById(R.id.rl), "Permission denied",
Snackbar.LENGTH_LONG).show();

}
}

To prompt user to grant or deny the permission, you can write the following code and it will automatically show a dialog to the user and give him/her the option to allow or deny. please mark that this dialog is system defined and uneditable.

requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);

Output will be like

Sample Image

Can't get WRITE_SETTINGS permission

To use WRITE_SETTINGS, based on the docs:

  1. Have the <uses-permission> element in the manifest as normal.

  2. Call Settings.System.canWrite() to see if you are eligible to write out settings.

  3. If canWrite() returns false, start up the ACTION_MANAGE_WRITE_SETTINGS activity so the user can agree there to allow your app to actually write to settings.

In other words, writing to settings is now a double-opt-in (agree to install, agree separately in Settings to allow), akin to device admin APIs, accessibility services, etc.

Also note that I have not tried using these yet — this is based on research that I did yesterday on Android 6.0 changes.



Related Topics



Leave a reply



Submit