Android location permissions not requesting and automatically denied - android

The first few times I tried the code out, it prompted me each time requesting for the permissions but after a while, it would just go straight to my manual input page.
Also, I'm not sure if it would be relevant but my targetSDKVersion is 30.
Please and thank you for the help :)
I included these permissions in my AndroidManifext.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
The general flow of my code is that it'll check the permissions like so
private boolean checkPermissions() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
And then, if the permissions are not granted, it should request the user for their location calling requestPermissions()
private void requestPermissions() {
Log.d(TAG, "requestPermissions");
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION}, PERMISSION_ID);
}
And after that:
// If everything is alright then
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_ID) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getCurrentLocation();
} else {
//if user denies permission to use GPS services
//update UI accordingly for manual input
}
}
But it doesn't request, and I don't know why. I've read the documentation here https://developer.android.com/training/location/permissions, but I just don't get it :\

I've been searching for this for a long time. You first have to ask for basic location permissions, when those granted, then ask for background location permission. It will take the user to phone settings page to allow all the time.
private void LocationPermission() {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Toast.makeText(MainActivity.this, R.string.permission_granted, Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, LOCATION_CODE2);
}else{
getLocation();
}
} else {
Toast.makeText(this, "Location access is required!", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == LOCATION_CODE2) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Toast.makeText(MainActivity.this, R.string.permission_granted, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Location access granted!", Toast.LENGTH_SHORT).show();
getLocation();
} else {
Toast.makeText(this, "Location access is required!", Toast.LENGTH_SHORT).show();
}
}
}

Helloo~
I've removed the permission requests for Manifest.permission.ACCESS_BACKGROUND_LOCATION and it workds now :O!!
Thanks to Pawel for pointing it out :D

Related

Ask Location & Storage permission again in Android

I'm building my android app with minimum api level 23. My app requires storage(READ/WRITE) and current location permission. When user access has been denied 2 times, then the dialog box is not popping up again.
I want to show the popup again & again instead of sending user to app-info to give permission manually after denying. Is that possible?
My Storage permmision code:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
this.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 2);
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 2: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Permission denied!", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
My Location permission code:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Permission denied!", Toast.LENGTH_SHORT).show();
}
}
}
No, it's not possible. You should display some popup informing the user that your app requires this permission and then after clicking a positive button you should move the user to the app settings.
The reason for that is after denying permission twice the status is set for "denied forever" and the ActivityCompat.shouldShowRequestPermissionRationale(activity, permission) method will return false.
Some apps force users to give permission by disabling a particular feature of the app. For example, after clicking "show on the map" button just request permission and in case of denial show a popup that the app requires this permission and the user can't open the map view.

Can't access camera on API < 23 devices with app having permissions

I'm developing an android app and i have problems with the camera permission.
On click of a button i call this
if (android.os.Build.VERSION.SDK_INT > 23) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
}
} else {
BarCodeReaderActivity.startActivity(this, REQUEST_CODE_BAR_CODE, mStatus);
}
}else{
if (cameraAvailable)
BarCodeReaderActivity.startActivity(this, REQUEST_CODE_BAR_CODE, mStatus);
else {
Toast.makeText(this, KanbanBoxSettings.getInstance(this).getTranslationString(Strings.message_camera_not_available), Toast.LENGTH_LONG).show();
}
}
When i look on
Settings > Apps > "Your app" > Permissions i see that the app has the camera permission so why is the camera still unavailable?
I don't know if this errorlog is usefull but here is what i get:
E/StopWatchTimer: [LOG_ERR]StopWatchTimerStart : 63 - StopWatchTimer have already start!
Call this method before executing your camera code.
public void checkLocationPermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ){
// You don't have the permission you need to request it
ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CODE_LOCATION);
} else {
// You have the permission.
setUserLocation();
}
}
Then, Override onRequestPermissionsResult
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE_LOCATION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
setUserLocation();
} else {
toast(getApplicationContext(),"You have cancelled location accessed request");
currentLoaction.setImageDrawable(getResources().getDrawable(R.drawable.location));
}
}
}
Now call your camera method. If this is not working then post your camera calling code.

Writing exception to parcel java.lang.SecurityException: com.android.phone from uid 10142 not allowed to perform OP_READ_PHONE_STATE

While scrolling and jump onto one fragment to another, my logcat shown this error and my app crashed and restart. Please suggest me any solution regarding this?
Add in Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
In activity page
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
} else {
//TODO
}
Permissions Result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do here
} else {
Toast.makeText(mContext, "The app was not allowed to write to your storage.", Toast.LENGTH_LONG).show();
}
}
}
}
For more details, please refer the following link
http://www.theappguruz.com/blog/runtime-permissions-in-android-marshmallow

error permision ACCESS_FIND_LOCATION

I'm using permission ACCESS_FINE_LOCATION but when run.error:requires ACCESS_FINE_LOCATION permission
why?
thanks in advance.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION both permission required when to trying to get location
so add this both permission in manifest file as well as if you targeting android 6.0 and above than add this runtime permsiion like below code
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);
}
now handle permisiion result
#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 ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "location_permission_not_granted ", Toast.LENGTH_SHORT).show();
}
}
}
Did you write run time permissions in your code? As from Android 6.0, for every permission stated in manifest, must also be asked run time from user.
private void requestLocationPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
}
}
Also handle user's response as well:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_CODE: {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
return;
}
}
}

How get permission for camera in android.(Specifically Marshmallow)

Hey I am designing an app in android studio. In which i require permission of camera. I have included <uses-permission android:name="android.permission.CAMERA" />
in the the AndroidManifest.xml file. It is working properly in all versions of android except Marshmallow. How do I get camera permission by default? If not possible how do I ask it from user?
First check if the user has granted the permission:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
Then, you could use this to request to the user:
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);
And in Marshmallow, it will appear in a dialog
You can try the following code to request camera permission in marshmallow. First check if the user grant the permission. If user has not granted the permission, then request the camera permission:
private static final int MY_CAMERA_REQUEST_CODE = 100;
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
After requesting the permission, dialog will display to ask permission containing allow and deny options. After clicking action we can get result of the request with the following method:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
This works for me, the source is here
int MY_PERMISSIONS_REQUEST_CAMERA=0;
// Here, this is the current activity
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
{
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA );
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
check using this
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)
and
I try to add following code:
private static final int MY_CAMERA_REQUEST_CODE = 100;
#RequiresApi(api = Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
On onCreate Function and this following code:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
And this worked for me :)
One of the easiest way to take permission is to used the deterex library. like below
Implement the following library in your gradle.
implementation 'com.karumi:dexter:6.2.2'
after adding the library used this for permission like below
Dexter.withContext(this)
.withPermissions(
Manifest.permission.CAMERA
).withListener(new MultiplePermissionsListener() {
#Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
#Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/*
... */}
}).check();
Don't forget to add the user permission in your manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Requesting Permissions
In the following code, we will ask for camera permission:
in java
EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting Android M or higher.
Installation
EasyPermissions is installed by adding the following dependency to your build.gradle file:
dependencies {
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
}
private void askAboutCamera(){
EasyPermissions.requestPermissions(
this,
"A partir deste ponto a permissão de câmera é necessária.",
CAMERA_REQUEST_CODE,
Manifest.permission.CAMERA );
}
click here for full source code and learn more.
Before get permission you can check api version,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
}
else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 401);
}
}
else
{
// if version is below m then write code here,
}
Get the Result of the permission dialog,
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 401) {
if (grantResults.length == 0 || grantResults == null) {
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openGallery();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
} else if (requestCode == 402) {
if (grantResults.length == 0 || grantResults == null) {
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
}
}
private const val CAMERA_PERMISSION_REQUEST_CODE = 2323
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED)
{
requestPermissions(arrayOf(Manifest.permission.CAMERA),
CAMERA_PERMISSION_REQUEST_CODE)
}
}

Categories

Resources