Trying to prompt user to set permissions on app launch in AndroidManifest - android

Okay, so in my AndroidManifest.xml file, I'm trying to set my permissions such that when the App launches, the user is asked to allow Location + Storage permissions.
I'm working off the BluetoothLeGatt example, and I used the uses-permission-sdk-23 tags to do it.
For reference, here's my code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bluetoothlegatt"
android:versionCode="1"
android:versionName="1.0">
<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
<!-- Declare this required feature if you want to make the app available to BLE-capable
devices only. If you want to make your app available to devices that don't support BLE,
you should omit this in the manifest. Instead, determine BLE capability by using
PackageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE) -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.BLUETOOTH"/>
<uses-permission-sdk-23 android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#android:style/Theme.Holo.Light">
<activity android:name=".DeviceScanActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DeviceControlActivity"/>
<service android:name=".BluetoothLeService" android:enabled="true"/>
</application>
</manifest>

try this
step 1 :- add permission that you want in manifiest file like this
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
step 2 : ask runtime permission like this
String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.
checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
{permission}, PERMISSION_GPS_CODE);
}
step 3: handle permission result like this
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_GPS_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, location_permission_granted_msg, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, location_permission_not_granted_msg, Toast.LENGTH_SHORT).show();
}
}
}

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
In MainActivity add the following
if (Build.VERSION.SDK_INT < 23) {
//We already have permission. Write your function call over hear
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Here we are asking for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
//If the app is running for second time, then we already have
permission. You can write your function here, if we already have
permission.//
}
}
do same for memory storage..

Related

Need example android Bluetooth permission code

I need an example java code of Bluetooth permissions. Android 12.
I know that I have to put this in the AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH"/>
From my research I think I have to use these tools to get my phone to ask me for Bluetooth permissions:
ActivityCompat
ContextCompat
PackageManager
onRequestPermissionsResult
But no luck after many hours of tests.
I even tried using libraries from GitHub to ask for "easy" permissions for me but no luck:
https://github.com/googlesamples/easypermissions
https://github.com/guolindev/PermissionX
I tried lowering my target SDK version so I don't need to ask for the user entry but "AndroidX" gave me an error telling that its not compatible with lower SDK's.
It would help me a ton if you have a basic working Bluetooth app java code so I can figure out how to code my own app.
For the new Bluetooth permissions on Android 12, you need these permissions:
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
You can refer to this blog post for more details.
For bluetooth permission on Android 12, you need these permission, depend on your usage:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
In your activity, you can add this to check the permission is allowed or not:
private static final int ACCESS_COARSE_LOCATION_RESULT_CODE = 4;
private static final int BLUETOOTH_RESULT_CODE = 5;
private static final int DANGEROUS_RESULT_CODE = 1;
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
ACCESS_COARSE_LOCATION_RESULT_CODE);
}
else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH},
BLUETOOTH_RESULT_CODE);
}
else if(ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_SCAN},
DANGEROUS_RESULT_CODE);
}
}
else if(ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT},
DANGEROUS_RESULT_CODE);
}
}
To understand more, you can check the documentation.

"Required permission was denied!" when requesting WRITE_EXTERNAL_STORAGE

I am trying to request WRITE_EXTERNAL_STORAGE permission on Android 10. I do that using the following code:
ArrayList<String> permissions = new ArrayList<>();
if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (this.checkSelfPermission(ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
this.checkSelfPermission(ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() > 0) requestPermissions(permissions.toArray(new String[0]), PERMISSION_REQUEST_FINE_LOCATION);
Here is the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
All other permissions work except WRITE_EXTERNAL_STORAGE. When I try to request it, it does not display any dialog for confirming permission, but it simply denies it. Why and how can I fix it?
I have discovered the solution, however the cause of the problem is still a mystery to me. If anyone has an explination, it would be greatly appreciated. Basically my issue is a duplicate of this issue. All I had to do is to change the following line from
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
What does tools:node="replace" do?
You can handle requested permission using this method (works with android 10), Here is the documentation.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) {
case PERMISSION_REQUEST_ID_STORAGE:
// permisstion granted by user
break;
}
} else {
switch (requestCode) {
case PERMISSION_REQUEST_ID_STORAGE:
// permission denied by user
break;
}
}
}
You can use Android Runtime Permission Library by nabinbhandari
in you build.gradle app module file write this
//Android Runtime Permission Library
implementation 'com.nabinbhandari.android:permissions:3.8'
In your Manifest file write
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and in Activity declare these variables
String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
String rationale = "Please provide storage permission to proceed ...";
And in your onCreate() Method write this
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(MainActivity.this, permissions, rationale, options, new PermissionHandler() {
#Override
public void onGranted() {
// do your task.
Toast.makeText(MainActivity.this, "Permissions granted", Toast.LENGTH_SHORT).show();
}
#Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied
Toast.makeText(MainActivity.this, "Permissions denied", Toast.LENGTH_SHORT).show();
}
});

Problem After Permission Change in Publish App

I have a android app in play store. That has only internet permission. Now I change in app user can use their camera & storage.
I add privacy policy on play store and in link in app also. I also mention these permission in release text.
When I upload new app display message like this
minSdkVersion 21
targetSdkVersion 28
versionCode 4
versionName "4.0"
Permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Permission Code
private void permission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(StartActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
//Display_Rermission_Reason();
}else {
Intent intent = new Intent(StartActivity.this, PrivacyPolicyActivity.class);
startActivity(intent);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// close the app
Toast.makeText(MainActivity.this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
//Display_Rermission_Reason();
}
}
}
After publish the app it display in google play store
This app is incompatible with all of your devices.
Any version 5+ and above
Change this in manifest,
<uses-feature android:name="android.hardware.camera2.full"
android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false" />
Have a look at this link below which might help you,
https://developer.android.com/guide/topics/manifest/uses-feature-element

missing permissions required by BluetoothAdapter.isEnabled.BLUETOOTH

I add bluetooth to my app but am running into the following problem. When I do the code:
BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}
if(!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
The error is on the following line:
if(!bluetoothAdapter.isEnabled())
Error:
Missing permissions required by BluetoothAdapter.isEnabled :android.permissions.BLUETOOTH
I added the following permissions to the android manifest file:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
but am still getting the same issue. Do you know what I did wrong?
You only need <uses-permission> tag since you are using a permission. You don't need <permissions> tag since this is not your custom permission.
Maybe you have placed <uses-permission> tag in the wrong element in manifest, but I cannot say anything else with the information you provided
Since your bluetoothAdapter can be null, either add to second IF null pointer check, or add a RETURN in first IF
It is also nice to have <uses-feature> tag
Let's say you have a button and then you click on it, Bluetooth needs to turn on. Here's a quick example how you would do it for Android 6.0 and above.
First, declare this variable in your Activity/Fragment:
public static final int PERMISSION_ASK = 1001;
Now, let's say this buttons is going to enable the bluetooth.
setOnClickListener:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(isBluetoothPermissionGranted()) {
// app already has required permissions
// do some task here
} else {
// app does not have permission yet.
// ask for permissions
askForBluetoothPermissions();
}
} else {
// Android version below 6.0, no need to check or ask for permission
// do some task here
}
}
});
Checking if application already has the required permission:
#RequiresApi(api = Build.VERSION_CODES.M)
private boolean isBluetoothPermissionGranted() {
boolean granted = false;
int bluetoothGranted = checkSelfPermission(Manifest.permission.BLUETOOTH);
int bluetoothAdminGranted = checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN);
if(bluetoothGranted == PackageManager.PERMISSION_GRANTED &&
bluetoothAdminGranted == PackageManager.PERMISSION_GRANTED) {
granted = true;
}
return granted;
}
If required permission is not granted, here's how you would ask for it:
#RequiresApi(api = Build.VERSION_CODES.M)
private void askForBluetoothPermissions() {
String[] permissions = new String[] {
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
};
requestPermissions(permissions, PERMISSION_ASK);
}
And finally, here's how you would confirm if the user granted you the permission(s) or not:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_ASK:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// all requested permissions were granted
// perform your task here
} else {
// permissions not granted
// DO NOT PERFORM THE TASK, it will fail/crash
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
Hope this helps
For Android 6.0 and above you must add the following to manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted

I am trying to utilize GPS in Android (2.2 and 2.3) but am getting the following error when I try to use the LocationManager object:
WARN/System.err(522): java.lang.SecurityException: Provider network requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission
I have researched the issue in other posts and they are typically due to issues with AndroidManifest.xml. However, mine appears to be fine:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thedigitalsean.examples"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".GetLocation"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permisssion.INTERNET"></uses-permission>
</manifest>
Here is the offending line of code in my onCreate method in the GetLocation Activity, encased in a try/catch block:
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener locListener = new MyLocListener();
try{
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
catch (SecurityException se){
((TextView)findViewById(R.id.infobox)).setText(se.toString());
se.printStackTrace();
}
This is the first app I have written where I need to request android permissions and have so far been unsuccessful in being granted any permissions whatsoever. Is there something I am missing in my manifest or perhaps in my eclipse configuration?
You misspelled permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I was having the same problem and could not figure out what I was doing wrong. Turns out, the auto-complete for Android Studio was changing the text to either all caps or all lower case (depending on whether I typed in upper case or lower cast words before the auto-complete). The OS was not registering the name due to this issue and I would get the error regarding a missing permission. As stated above, ensure your permissions are labeled correctly:
Correct:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Incorrect:
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_FINE_LOCATION" />
Incorrect:
<uses-permission android:name="android.permission.access_fine_location" />
Though this may seem trivial, its easy to overlook.
If there is some setting to make permissions non-case-sensitive, please add a comment with the instructions. Thank you!
Compatible with all SDK versions (android.permission.ACCESS_FINE_LOCATION became dangerous permission in Android M and requires user to manually grant it).
In Android versions below Android M ContextCompat.checkSelfPermission(...) always returns true if you add these permission(s) in AndroidManifest.xml)
public void onSomeButtonClick() {
...
if (!permissionsGranted()) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 123);
} else doLocationAccessRelatedJob();
...
}
private Boolean permissionsGranted() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
#Override
public void onRequestPermissionsResult(final int requestCode, #NonNull final String[] permissions, #NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted.
doLocationAccessRelatedJob();
} else {
// User refused to grant permission. You can add AlertDialog here
Toast.makeText(this, "You didn't give permission to access device location", Toast.LENGTH_LONG).show();
startInstalledAppDetailsActivity();
}
}
}
private void startInstalledAppDetailsActivity() {
Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I Had an strange issue and realized that the text of manifest permission has a a space character at the end. removed it and it is working just fine.
Before
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION "/>
After
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
just remove s from the permission you are using sss you have to use ss

Categories

Resources