Marshmallow permission - android

I'm trying to check permission in activity as below,
int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNT);
But I'm getting error at GET_ACCOUNT as Cannot resolve symbol 'GET_ACCOUNT'.
Kindly help what code should I add to fix this?
Thanks in advance.

It should be
Manifest.permission.GET_ACCOUNTS
instead of
Manifest.permission.GET_ACCOUNT
Try like this
int permission = ContextCompat.checkSelfPermission(this,Manifest.permission.GET_ACCOUNTS);
Don't forget to add permission on your Manifest
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
Import this library in your class
import android.Manifest;

First GET_ACCOUNT is not defined in android.Manifest.permission, it is GET_ACCOUNTS
Second, change your Manifest file to use GET_ACCOUNTS
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
then in your code file i.e. java file write like this.
ContextCompat.checkSelfPermission(this, android.Manifest.permission.GET_ACCOUNTS);
Check documentation GET_ACCOUTNS.

The following code worked for me,
Declare below code as:
public static final int PERMISSIONS_REQUEST_GET_ACCOUNT = 133; // any number
Call following code in your onCreate method:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.GET_ACCOUNTS},
PERMISSIONS_REQUEST_GET_ACCOUNT);
}else{
//furtherCode
}
}else{
//furtherCode
}
in method onRequestPermissionsResult method,
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case PERMISSIONS_REQUEST_GET_ACCOUNT :
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Granted", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "No such permission to access account!", Toast.LENGTH_SHORT).show();
}
break;
}
}

Related

Not asking external storage permission on Android studio

my name is Tom, i am trying to open a file from the external storage. for some reason it is not asking the permission at all?!?!
I will thank for any answer!
Tom.
Here is my code:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this,"should snd request ",Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUET_CODE);
}
public void OnRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResult){
if(requestCode == PERMISSION_REQUET_CODE){
if(grantResult[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this,"permission granted!",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"*** permission not granted! ***",Toast.LENGTH_LONG).show();
finish();
}
}
}
Make sure you also have added permissions to the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appname">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>

Disconnect from a wifi network programmatically in Marshmallow

Disconnect from a wifi network programmatically in Marshmallow
Unable to a disconnect from a wifi network programmatically in marshmallow can not find any solution. If any one is having any solution will be huge help .Thank you
UPDATE
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
wm.disconnect();
Also set permissions to Change Wifi State in Manifest and also at Runtime for devices running Android 6.0
Inside Manifest.xml
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Check permission at Runtime:
if (ActivityCompat.checkSelfPermission(SmsOtpCheck.this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CHANGE_WIFI_STATE}, CODE);
After approving or rejecting permission:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
//PERMISSION GRANTED
else
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
}
}

Doesn't work on Android Studio- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

I'm trying to find make the following line work:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
The studio marks it as Error and suggests to check that all permissions are set or handling a SecurityException.
I think that I have set all the necessary permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
But still , it does not work.
Did someone have the same problem ?
Thanks in advance !
you can use these code.In android studio you have to check dynamic permission at run time
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
getLocation();
}
and
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
}
break;
}
}
This is because in Marshmallow, permissions are asked at runtime to the user.So we need to handle wheather user has granted that particular permission or not.
Add these two permissions:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>

onRequestPermissionsResult returns immediately with denied permission

My Android app needs to request permission for location services. I do this with:
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_CODE_ACCESS_COARSE_LOCATION);
But immediately after this is called, onRequestPermissionsResult returns immediately with permission denied:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ACCESS_COARSE_LOCATION: {
}
}
}
The permission is listed in the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
I also cleared the cache to prevent any previous denial the user gave from affecting this.
I am expecting a dialog to pop up requesting the user to grant or deny permission to the location services but it isn't showing.
Change:
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
to:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
The permission is for "coarse" location data (i.e., not fine-grained), not "course" location data (e.g., where some university class is being held).
private int PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 100;
private void checkPermission() {
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(newString[{Manfest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
} else {
doShowLocation();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showContacts();
}
}
One more scenario that onRequestPermissionsResult can return denied permission even before permission dialog appears is when you declared some other permission in android manifest and asking some other permission in run time. Example:
You have declared
<uses-permission android:name="android.permission.READ_SMS" />
in android manifest file. However you are trying to get
Manifest.permission.SEND_SMS
in runtime permission. So making the runtime permission to READ_SMS will work without any issues.

Neither user 10102 nor current process has android.permission.READ_PHONE_STATE

I am trying to call getCallCapablePhoneAccounts() method of android.telecom.TelecomManager class. Though i have added required user-permission, i am getting Security exception.
Here is the line of code where i am getting exception
List<PhoneAccountHandle> list = getTelecomManager().getCallCapablePhoneAccounts();
user permission added in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Exception stacktrace
Caused by: java.lang.SecurityException: getDefaultOutgoingPhoneAccount: Neither user 10102 nor current process
has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telecom.ITelecomService$Stub$Proxy.getDefaultOutgoingPhoneAccount(ITelecomService.java:615)
at android.telecom.TelecomManager.getDefaultOutgoingPhoneAccount(TelecomManager.java:439)
On Android >=6.0, We have to request permission runtime.
Step1: add in AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Step2: Request permission.
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
}
Step3: Handle callback when you request permission.
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_READ_PHONE_STATE:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
//TODO
}
break;
default:
break;
}
}
Edit:
Read official guide here Requesting Permissions at Run Time
Are you running Android M? If so, this is because it's not enough to declare permissions in the manifest. For some permissions, you have to explicitly ask user in the runtime: http://developer.android.com/training/permissions/requesting.html
I was experiencing this problem on Samsung devices (fine on others). like zyamys suggested in his/her comment, I added the manifest.permission line but in addition to rather than instead of the original line, so:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.Manifest.permission.READ_PHONE_STATE" />
I'm targeting API 22, so don't need to explicitly ask for permissions.

Categories

Resources