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.
Related
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")
}
I am working on an Android app that uses the phone's camera to take pictures. Upon install and launching the camera on the emulator for the first time, the following permission is requested:
App Screenshot
It is a bit disruptive to the app. I already have other permissions set on the manifest:
<uses-permission android:name="android.permission.CAMERA" />
<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_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
And on the main activity prior to login:
private boolean checkPermissions() {
if ( ContextCompat.checkSelfPermission( getApplicationContext(),
Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission( getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission( getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED ) {
return false;
}
return true;
}
private void requestPermission() {
ActivityCompat.requestPermissions( this,
new String[]{ Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE },
PERMISSION_REQUEST_CODE );
}
Is there a way to make the request "Allow Camera to access this device's location" alongside with my other permissions before a user is allowed to log in?
I have also attempted to request permission for the fine location:
private boolean checkPermissions() {
if ( ContextCompat.checkSelfPermission( getApplicationContext(),
Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission( getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission( getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission( getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
return false;
}
return true;
}
private void requestPermission() {
ActivityCompat.requestPermissions( this,
new String[]{ Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION },
PERMISSION_REQUEST_CODE );
}
Which grants my app, and not the camera, the permission beforehand which doesn't really solve my problem. Any suggestions are appreciated.
Sorry for late reply but I was a bit confused about this myself:
I think this is not actually your app asking for permission, but rather the android camera app asking for permission, so there is not much to do. If you were to launch the camera app on a clean emulator you would get the same result.
Hopefully though your apps users will already have used the camera before installing you app and this shouldn't be a problem. :)
Do manifest permissions like "SEND_SMS" override the app permissions in Settings like "SMS" if it is set to off? The don't seem to set the later to on.
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
SMS for the app settings set to "on" is expected. No error messages are being displayed.
Check all the permissions at runtime...in your MainActivity, so that if permissions are being off from settings then it will again ask for permissions when you open the application.
Call this method in onCreate() of your MainActivity.
private boolean checkAndRequestPermissions() {
int SEND_SMS = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);
int RECEIVE_SMS = ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);
int READ_SMS = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
List<String> listPermissionsNeeded = new ArrayList<>();
if (SEND_SMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (RECEIVE_SMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS);
}
if (READ_SMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_SMS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
(new String[listPermissionsNeeded.size()]), 101);
return false;
}
return true;
}
I have used the permissions in manifest file and permissions during runtime too.
But i still get the error
"Missing permissions required by LocationManager.requestLocationUpdates: android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION
Inspection info:This check scans through your code and libraries and looks at the APIs being used, and checks this against the set of permissions required to access those APIs. If the code using those APIs is called at runtime, then the program will crash. Furthermore, for permissions that are revocable (with targetSdkVersion 23), client code must also be prepared to handle the calls throwing an exception if the user rejects the request for permission at runtime.
Issue id: MissingPermission"
I'm using Android Studio 3.4.1
Manifest file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
In the Activity:
public void getLocation()
{
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 5000, 5, this);
}
catch(SecurityException e) {
e.printStackTrace();
}
}
In the onCreate() method:
if (ContextCompat.checkSelfPermission(getApplicationContext(),android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(),android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
Please help me out. I have gone through posts related to this but doesn't help me at all.
Try the following,
#TargetApi(23)
public void getLocation()
{
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 5000, 5, this);
}
catch(SecurityException e) {
e.printStackTrace();
}
}
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" />