Permission popup doesn't show on Android 7.0, shouldShowRequestPermissionRationale always false - java

I have a react native app, I use this java code to check the camera permission :
int permission = PermissionChecker.checkSelfPermission(getReactApplicationContext(), Manifest.permission.CAMERA);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA);
}
promise.resolve(permission);
On Android 6.0, when the camera permission is off, the native permission popup shows thanks to the requestPermissions function.
However, it doesn't show on Android 7.0.
I checked the result of :
ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)
It's false on Android 6.0 and 7.0
Thanks for your answer,

This is how we check and manage all the end user input when dealing with permissions just change to the permission you want to deal with
First check
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
} else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 666); // Comment 26
}
}
Then manage end user input
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 666: // User selected Allowed Permission Granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar s = Snackbar.make(findViewById(android.R.id.content), "Permission Granted", Snackbar.LENGTH_LONG);
View snackbarView = s.getView();
snackbarView.setBackgroundColor(Color.WHITE);
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.RED);
textView.setTextSize(18);
textView.setMaxLines(6);
s.show();
// do your work here
// User selected the Never Ask Again Option Change settings in app settings manually
} else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
alertDialogBuilder.setTitle("Change Permissions in Settings");
alertDialogBuilder
.setMessage("Click SETTINGS to Manually Set\n\n" + "Permissions to use Database Storage")
.setCancelable(false)
.setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 1000);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
// User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
alertDialogBuilder.setTitle("Second Chance");
alertDialogBuilder
.setMessage("Click RETRY to Set Permissions to Allow\n\n" + "Click EXIT to the Close App")
.setCancelable(false)
.setPositiveButton("RETRY", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
})
.setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
break;
}};

Related

Android Studio - not asking Write_External_Storage

How I'm requesting
public void requestPermissionToWriteToFile() {
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed for camera use and internal file save")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_FILES);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
else
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_FILES);
}
}
How I'm Checking
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == PERMISSION_REQUEST_WRITE_FILES){
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();}
}
}
As soon as the app boots up, the accesses is never granted and it just stays that way, when I call on the above method the dialog for permission wont come up (the permission isn't being asked). Even when I go to settings and change it from there it still won't grant me permission to WRITE_EXTERNAL_STORAGE. the above code is in my main class, the permission is in Manifest, what else is needed to fix this?
there was another thread like this but that didn't solve my problem jsyk.
Before trying the following code, add the missing G in STORAGE.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then call this when your button is clicked:
if (androidx.core.content.ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_DENIED || androidx.core.content.ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_DENIED) {
androidx.core.app.ActivityCompat.requestPermissions(MainActivity.this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
} else {
//permission already granted
}
Also, add read external storage permission to avoid any issues:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

App closed when it asks permission to user for read write

I am facing issues in my app, My app needs read write permission but whenever my app opens first time my app crashes and closed then it asks for permission in background then i need to start it again then it works fine. I am getting issues only first time run.
This is my code
Main Activity
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
checkPermissions();
}
#RequiresApi(api = Build.VERSION_CODES.M)
private void checkPermissions(){
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
}
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1052);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
}
/* if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= android.content.pm.PackageManager.PERMISSION_GRANTED||
ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= android.content.pm.PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
},
1052);*/
}
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1052: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED
&& grantResults[1] == android.content.pm.PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "Enjoy This app", Toast.LENGTH_SHORT).show();
}
else {
android.widget.Toast.makeText(this, "Permission Denied, without your permission we cant share or download images to your device", android.widget.Toast.LENGTH_LONG).show();
// Permission denied - Show a message to inform the user that this app only works
// with these permissions granted
}
}
}
}
You need to ask for permission by starting a new activity and writing all the above code in that new activity. I replicated this problem in a new android project and verified this.
What you need to do is create another activity, one that is specifically created to ask for permissions. Let it be Main2Activity.java. Your MainActivity.java would be
public class MainActivity extends AppCompatActivity {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
}
}
Then your Main2Activity.java would be
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
checkPermissions();
}
}
#RequiresApi(api = Build.VERSION_CODES.M)
private void checkPermissions(){
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
}
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1052);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
}
/* if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= android.content.pm.PackageManager.PERMISSION_GRANTED||
ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= android.content.pm.PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
},
1052);*/
}
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1052: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& (grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED
|| grantResults[1] == android.content.pm.PackageManager.PERMISSION_GRANTED))
{
Toast.makeText(this, "Enjoy This app", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Main2Activity.this, MainActivity.class));
finish();
}
else {
android.widget.Toast.makeText(this, "Permission Denied, without your permission we cant share or download images to your device", android.widget.Toast.LENGTH_LONG).show();
// Permission denied - Show a message to inform the user that this app only works
// with these permissions granted
}
}
}
}
}
Notice that in Main2Activity.java, i made edits
if (grantResults.length > 0
&& (grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED
|| grantResults[1] == android.content.pm.PackageManager.PERMISSION_GRANTED))
{
Toast.makeText(this, "Enjoy This app", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Main2Activity.this, MainActivity.class));
finish();
}
For the Toast to appear, android.content.pm.PackageManager.PERMISSION_GRANTED will be either in grantResults[0] or grantResults[1] but not both.
I used functions startActivity() and finish() because after getting permission this activity made specifically for permission will close and open the MainActivity.java.

I'm Working with the action call in the android

I've done the code for placing the call and the thing is i want to place the call from the number which i stored in the database means i want dynamic calling action in my app...
Hoe do i do that....Any Help would be appreciated......
THANKS IN ADVANCE!!!!!
My code for placing the callsss......is right under below....
cardView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9855956530"));
startActivity(callIntent);
if (Build.VERSION.SDK_INT >= 23) {
if (getActivity().checkSelfPermission(android.Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("TAG","Permission is granted");
} else {
Log.v("TAG","Permission is revoked");
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, 1);
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v("TAG","Permission is granted");
}
}
});
And i used the fragments in this app
Database is Sqlite Database

SecurityException Permission - not saving image to android device

Trying to make a painting app. Everything but the save button is functioning as intended. Whenever I click the save button, it's not saving and I'm getting an error java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=10397, uid=10298 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission().
else if(view.getId()==R.id.save_btn){
//save drawing
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save drawing");
saveDialog.setMessage("Save drawing to device Gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
//save drawing
}
});
saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
saveDialog.show();
drawView.setDrawingCacheEnabled(true);
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), drawView.getDrawingCache(),
UUID.randomUUID().toString()+".png", "drawing");
In the manifest I have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
you can just wrap your code like this:
//check permission is granted or no
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RESULT);
}
else
{
//your code
}
You have to write storage permission on Manifest fie.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also for Marshmallow you have to write code for checking and granting storage permission.
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
}
If you need to ask permission then below code works.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
Result callback for permission will be.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
}

Android onRequestPermissionsResult not working correctly

I have a weird bug that is driving me crazy. I'm working on Android Marshmallow and I'm implementing the new permissions. When I click my Login button, I check to see if the user has gps permissions. If not, I request them. The way the code works is that after permissions are asked, an Async task is called to get some settings from a REST service, then on the onPostExecute of the Async task, it will fire an Intent.
When the user Allows permissions, everything works fine. If the user denies permissions, it will call the Async task and call the Intent, but it will not activate the Intent. It simply stays on the screen.
The Button Click
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
int has_permission = 0;
if (Build.VERSION.SDK_INT >= 23)
{
has_permission = Check_Permission(Manifest.permission.ACCESS_FINE_LOCATION);
}
action = "login";
if(has_permission == 0)
{
Get_Location();
load_system_settings_async = new Load_System_Settings_Async();
load_system_settings_async.execute((Void) null);
}
}
});
Check Permission Code
protected int Check_Permission(final String permission)
{
int has_permission = checkSelfPermission(permission);
if (has_permission != PackageManager.PERMISSION_GRANTED)
{
if (!shouldShowRequestPermissionRationale(permission) && (request_times > 0))
{
String title="";
if(permission.equals(Manifest.permission.ACCESS_FINE_LOCATION))
{
title = "You need to allow GPS access";
}
else if(permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
title = "You need to allow storage access";
}
else if(permission.equals(Manifest.permission.CALL_PHONE))
{
title = "You need to allow access phone access";
}
Show_Alert_Dialog("Ok", title,
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//requestPermissions(new String[] {permission}, REQUEST_CODE_ASK_PERMISSIONS);
}
});
return has_permission;
}
requestPermissions(new String[] {permission}, REQUEST_CODE_ASK_PERMISSIONS);
request_times++;
return has_permission;
}
return has_permission;
}
Permission Request
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults)
{
if (requestCode == 123)
{
if(grantResults[0] == 0)
{
Get_Location();
}
load_system_settings_async = new Load_System_Settings_Async();
load_system_settings_async.execute((Void) null);
//request_times = 0;
}
else
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
it seems like the problem is when user denied the permission ,control fall in onRequestPermissionsResult where you are executing your asynch task even if the permission is not granted .check comment in following code
public void onRequestPermissionsResult(int requestCode, #NonNull String[]
permissions, #NonNull int[] grantResults)
{
if (requestCode == 123) // yes request code is the same go on
{
if(grantResults[0] == 0) //yes we get the approval
{
Get_Location();
}
// oh even if we didn't get the approval we still gonna execute the task
load_system_settings_async = new Load_System_Settings_Async();
load_system_settings_async.execute((Void) null);
//request_times = 0;
}
else //control only come here when request code will not match
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
you need to combine the if condition like this
if (requestCode == 123 && grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Ok, I figured it out .. When I was passing a custom Object with putExtra in the Intent, there was a property that was null, so when it was executing writeToParcel in the object, it was crashing. Unfortunately, Android kept executing with out crashing .. it just did nothing. I simply added a setter to the object and set the value to false. Issue had nothing to do with the Permission code. Four hours of life and sleep lost that I will not get back. Thanks all for who viewed.

Categories