Run time Permission popup click issue in Marshmallow Updated devices - android

I integrated Android Runtime Permission for READ PHONE STATE for Marshmallow devices. This implementation is working fine and the popup is showing in my application with allow/deny option.
I can click the allow/ deny button for normal marshmallow devices. But in the case of updated android devices(From Lollipop to Marshmallow), the allow button click is not working initial time. This issue is tested and reproduced in Nexus 5 and Nexus 7. Is anything we need to create additional for updated OS? Or is it a Marshmallow issue?
Please check the complete code:
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 101;
if (Build.VERSION.SDK_INT >= 23) {
if(mActivity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) !=PackageManager.PERMISSION_GRANTED) {
if (this.shouldShowRequestPermissionRationale(
Manifest.permission.READ_PHONE_STATE)) {
showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
} else {
this.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
} else {
handleLoginAPI();
}
} else {
handleLoginAPI();
}
private void handleLoginAPI() {
if (super.isNetworkConnectionAvailable(mActivity)) {
// Api Call from here..
}else{
// No Nw Connection.
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
handleLoginAPI();
} else if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_DENIED) {
if (this.shouldShowRequestPermissionRationale(
Manifest.permission.READ_PHONE_STATE)) {
showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
}
}
return;
}
}
}

Don't call shouldShowRequestPermissionRationale() before requesting the permission. That call is intended to be made after the user has rejected the request, usually in the onRequestPermissionsResult() method. If your app needs to explain why it needs a permission before it asks for it, the app's internal logic needs to decide that.

private void setCheck()
{
int hasWriteContactsPermission = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
hasWriteContactsPermission = checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(android.Manifest.permission.READ_PHONE_STATE)) {
showLocationRationleDalog();
return;
}
return;
} else {
handleLoginAPI();
}
} else {
handleLoginAPI();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
handleLoginAPI();
} else {
Log.i("Permission denied");
}
return;
}
}
}

This is an Android OS update issue. The issue is reported in Nexus device. https://code.google.com/p/android/issues/detail?id=213120. The issue can be reproducible in other android native application as like Google map, Gmail etc.

Related

loading webview immediately after granting of permission

In our android webview app, We ask for read_contacts, location, camera, audio permissions at a time on initialization of app. What happens is that simultaneously the webview url is also loaded. This causes some crucial data like location not to be passed to webview in the first load.
What we expect from the app is to load the webview url immediately after user allows, grants permissions for the above only and not before that. We tried using onRequestPermissionsResult for achieving this, but unable to do so. The code we have tried is as given hereunder
if (!check_permission(4) || !check_permission(3)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_PHONE_NUMBERS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.RECORD_AUDIO,Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
contact_perm);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
get_location();
asw_view.loadUrl(url);
}
}
}
Any help would be appreciated.
You're checking only for 0th element, i.e., grantResults[0] == PackageManager.PERMISSION_GRANTED in onRequestPermissionsResult(...).
You need to check like this:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == 1) {
// Use this method to check if all the permissions are GRANTED.
if (areAllPermissionsGranted(grantResults)) {
get_location();
asw_view.loadUrl(url);
} else {
/*
* NOTE:
* -----
* Add a Log here to check if all the permissions are granted.
* If this block doesn't executes, it means all the permissions are granted,
* something else is wrong inside your 'if' block and you need to debug that block.
* */
}
}
}
// Method to check if all the results are GRANTED.
private Boolean areAllPermissionsGranted(int[] grantResults) {
boolean isGranted = false;
if (grantResults.length > 0) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
isGranted = true;
} else {
// if a single permission is NOT_GRANTED, return from the method.
return false;
}
}
}
return isGranted;
}
Use this library
gradle:
implementation 'com.nabinbhandari.android:permissions:3.8'
then:
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; //add your requested permissions to this array
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(this/*context*/, permissions, rationale, options, new
PermissionHandler() {
#Override
public void onGranted() {
// do your task here
}
#Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied, block the feature.
}
});

My application running at lollipop version but can't run at higher versions

My application running at lollipop version but can't run at higher versions. This error occurs:
The problem is that in 6.0 or above versions , you need to use runtime permissions.
Follow the sample code below :
public void checkPer()
{
if ((ContextCompat.checkSelfPermission(SubCategoryActivity.this,"android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
} else {
// user already provided permission
// perform function for what you want to achieve
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean canUseExternalStorage = false;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
canUseExternalStorage = true;
}
if (!canUseExternalStorage) {
Toast.makeText(SubCategoryActivity.this, "You cannot see images without requested permission", Toast.LENGTH_SHORT).show();
} else {
// user now provided permission
// perform function for what you want to achieve
}
}
}
}
This will help you my friend.

App close and background to be black when i ask permission for read phone state at run time in marshmallow

the below code is work only in M version of android, i get issue when i request for permission, after permission request system dialog will be open but its background to be black and after when i try to accept/deny i found app was closed.
#TargetApi(Build.VERSION_CODES.M)
public boolean checkPermissionPhoneState(final String manifestPermissionRequestState, final int PERMISSION_REQUEST_PHONE_STATE) {
int result = ContextCompat.checkSelfPermission(context,manifestPermissionRequestState);
if (result != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(manifestPermissionRequestState)) {
requestPermissions(new String[]{manifestPermissionRequestState}, PERMISSION_REQUEST_PHONE_STATE);
contactPermission = false;
}else{
requestPermissions(new String[]{manifestPermissionRequestState}, PERMISSION_REQUEST_PHONE_STATE);
contactPermission = false;
}
} else {
contactPermission = true;
}
return contactPermission;
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
contactPermission = true;
Snackbar.make(view, "Permission Granted", Snackbar.LENGTH_SHORT).show();
} else {
Snackbar.make(view, "Permission Denied", Snackbar.LENGTH_SHORT).show();
}
break;
}
}
I got answer, this app did not crash from background it will close automatically when dialog appear. the main issue was i include "android:noHistory='true'" in Android-manifest,when dialog appear app remove from stack. So after remove this tag from Manifest file my app work properly.

Android Marshmallow : activity recreates after permission granted

So when i call requestContactPermission method, the dialog appears normally but when i click on allow button , the fragment close and the activity forced to recreate ? what seems to be the problem ? the log dose not show anything
private void requestContactPermission() {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
getLoaderManager().initLoader(0, null, this);
} else {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
101);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLoaderManager().initLoader(0, null, this);
}
break;
}
}
my main problem is when i test it in emulator its works fine but when i test it in real device, like Galaxy S7 edge i'm having this problem
Give this a try.
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Requesting Runtime Permission Read contact");
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_CONTACTS},
101);
} else {
Log.d(TAG, "Previously User have provided Read contact access");
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 101: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Write External Storage Access granted");
downloadCurrentImage();
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
// other 'case' lines to check for other
// permissions this app might request
}
}

WifiManager.updateNetwork not working in Marshmallow

I have been using Wifimanager.updateNetwork(wifiConfiguration); in my app and it has been working on Kitkat, JellyBean & ICS, but now in Marshmallow it keeps returning -1 even when the wifiConfiguration is not null and has a valid networkId. Does anyone know the possible reasons for this ? Has something changed in Marshmallow regarding this ?
According to Android 6.0 Wi-Fi and Networking Changes,
Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
if(Build.VERSION.SDK_INT < 23){
//your code here
}else {
requestContactPermission();
}
private void requestContactPermission() {
int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_WIFI_STATE);
if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(Context, new String[]{Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.ACCESS_WIFI_STATE}, PERMISSION_REQUEST_CODE);
}else {
//Toast.makeText(AddContactsActivity.this, "Wifi Permission is already granted", Toast.LENGTH_LONG).show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
// Check if the only required permission has been granted
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Permission", "Wifi permission has now been granted. Showing result.");
Toast.makeText(this," Wifi Permission is Granted",Toast.LENGTH_SHORT).show();
} else {
Log.i("Permission", "Wifi permission was NOT granted.");
}
break;
}
}

Categories

Resources