Android application permission settings - android

My internal android application worked fine on Samsung Tab S2 and it automatically got permissions to access 'storage' and 'location' of the device. But, when I am installing the application on newer Samsung Galaxy Tab S7, I have to manually provide permission for the app in settings. By default it is set to "No permissions allowed" How cam I automate this.
AndroidManifest.xml has following settings
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Android permissions went through a radical change after android M was released. before that, developers can simply jam all the permissions in the AndroidManifesto file, and before the user installs the app, he/she has to approve all the permissions to run the app.
However, things are different now. Android expects that these permissions will be granted at rather runtime, i.e. when a feature that needs that permission is executed, it first requests for that permission, and based on user's reply handle that. If permission is granted, then good. If not, then simply display a message maybe stating that this feature will not work in the absence of that permission. In which case, because the user has already declined the permission, he/she now has to go to the settings to enable the previously declined permission.

You have to grant permission from the user. You can use it inside Fragment:
Java:
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(getActivity(),
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION);
} else {
Log.e("DB", "PERMISSION GRANTED");
}
Kotlin:
if (ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(getActivity(), arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_LOCATION)
} else {
Log.e("DB", "PERMISSION GRANTED")
}
and inside Activity:
Java:
int checkPermission= ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA);
if (checkPermission!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity)getContext(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
else {
Log.e("DB", "PERMISSION GRANTED")
}
Kotlin:
val checkPermission = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA)
if (checkPermission != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(getContext() as Activity, arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_LOCATION)
}
else
{
Log.e("DB", "PERMISSION GRANTED")
}

Related

storage permissions not being asked

I have added to the manifest files these permissions
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
But when the user installs the app the only permission he is being asked and allows is location.
I also want to ask the user for storage permission.How is this possible?
Use runtime permission code where you add the data inside the internal storage. You can use dexture or easy permission lib for this.
Have you tried what the Android documentation points out:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
https://developer.android.com/training/permissions/requesting#java
For "dangerous" permissions (includes Storage, Location, Camera, Calendar and more), you need to request the user for their permission not at installation, but when it's required.
It's important that you check every time the permission is required, because the user can revoke the permission anytime.
To check for permission (in Kotlin) (in this example it's FINE_LOCATION, change it to whatever permission you need to request):
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
) {
//if permission not granted, start function to request permission
//MY_PERMISSIONS_REQUEST is the requestCode, an int > 0
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
MY_PERMISSIONS_REQUEST
)
}
and then then override the onRequestPermissionsResult function:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
MY_PERMISSIONS_REQUEST -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
//granted - you are not able to use Location Functionality
} else {
// permission denied
Toast.makeText(this, "Location Functionality Disabled.", Toast.LENGTH_LONG).show()
}
return
}
// Add other 'when' lines to check for other
// permissions this app might request.
else -> {
// Ignore all other requests.
}
}
}
You can also request multiple permissions at once.
Android 6.0 multiple permissions

Runtime permission dialogue does not appear for SMS read

I need to request to Read permission but system permission dialogue does not appear. When I added the SMS Receive permission then dialogue appears but why it does not work on only SMS Read permission?
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_SMS)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{ Manifest.permission.READ_SMS}, 101);
}
Try with this
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(getActivity(),
new String[]{android.Manifest.permission.READ_SMS},
101);
} else {
Log.e("DB", "PERMISSION GRANTED");
}
Also make sure you have to add permission in Manifest.
<uses-permission android:name="android.permission.READ_SMS" />

Android Location Permission

I am using these two permissions in my application
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
I have added the permission in the manifest. However, when I debug into my phone, i try to check the permission using the function below:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.i("permission","done");
}
else
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION);
}
// The ACCESS_FINE_LOCATION is denied, then I request it and manage the result in
// onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
The application will enter the first IF statement, instead of requesting permission from the user.
I didn't grant any permission to this application before.
Why is the permission become GRANTED even if i just debug and install the application in my phone?
Because the runtime permission is added in Marshmallow and not in the previous versions, so if you have added the permission in the manifest it will automatically grant permission and don't even ask for the permission at runtime unless it's Marshmallow device.

Android: WRITE_EXTERNAL_STORAGE permission not accepted by device

I'm having a rather strange problem with an Android app I'm writing.
In my app, I use an intent to tell the phone's camera app to take a picture for me. After I take this picture, I want to register the newly taken photo with the device's MediaStore content provider in order to make it show up in the regular "gallery" app.
To do this, I know I need the WRITE_EXTERNAL_STORAGE permission, but even when it's in my manifest I get a permission denied exception. What do I have to do to get this permission? I'm running Android 6.0.1.
Here's my permissions XML in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Here's the code that causes the exception:
// insert photo into phone's main photo content provider
ContentResolver cr = getActivity().getContentResolver();
try {
MediaStore.Images.Media.insertImage(cr, photoFile.getPath(),
"Image Capture", "Custom Image capture");
} catch (FileNotFoundException foe) {
Log.e(DEBUG, foe.getMessage());
}
And here's the exception I get:
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2436, uid=10041 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
EDIT: I should probably mention:
Right now my app uses getExternalFilesDir(Environment.DIRECTORY_PICTURES), which returns the path /sdcard/Android/data/<package>/files/Pictures to save it's photos. I want them to go where the camera app normally puts them, in /sdcard/DCIM/Camera.
You should add the android:maxSdkVersion="18" to the WRITE_EXTERNAL_STORAGE as well. Your code should look like this:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
This may be a problem with respect to you requesting the permission but not being granted the permission by the user. Have a look at my answer # https://stackoverflow.com/a/35574084/529691
And see if that helps.
You should promt the user to provide you permission:
int externalPermissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (externalPermissionCheck==-1){
askPermissionStorage();
}
private void askPermissionStorage() {
//for media
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//insert explanation
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//if you want to explaian to the user
} else {//if no explanation needed
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
}
The above is for save the picture\video
and this is for the camera permission:
int cameraPermissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if (cameraPermissionCheck == -1) {
askPermissionCamera();
}
private void askPermissionCamera() {
//for camera
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
CAMERA) != PackageManager.PERMISSION_GRANTED) {
//insert explanation
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
//if you want to explaian to the user
} else {//if no explanation needed
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}

I am setting permissions in manifest but have to enable manually in `marshmallow`

Here is my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
This is all I needed for my project but I need it in code, not manually in app settings.
To use the new permission model in Marshmallow, you need to both specify your permissions in the manifest (as you did) AND request the permission at runtime. Only permissions that are considered as "dangerous" require the runtime request. Dangerous is defined as:
A higher-risk permission that would give a requesting application
access to private user data or control over the device that can
negatively impact the user. Because this type of permission introduces
potential risk, the system may not automatically grant it to the
requesting application. For example, any dangerous permissions
requested by an application may be displayed to the user and require
confirmation before proceeding, or some other approach may be taken to
avoid the user automatically allowing the use of such facilities.
From: https://developer.android.com/guide/topics/manifest/permission-element.html
Here's an example of code that checks if you need to request the permission. (Remember, even if the user grants your permission request, they can later revoke it in App Settings, so you need to check each time you need it.)
private final int REQUEST_PERMISSION_ACCESS_FINE_LOCATION=1;
private void requestAccessLocationPermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
showExplanation("Permission Needed", "Rationale", Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_PERMISSION_ACCESS_FINE_LOCATION);
} else {
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_PERMISSION_ACCESS_FINE_LOCATION);
}
} else {
Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onRequestPermissionsResult(
int requestCode,
String permissions[],
int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSION_ACCESS_FINE_LOCATION:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_SHORT).show();
}
}
}

Categories

Resources