0

I have written a sample code for easy understanding of run time permission. It is working fine. But the issue I m facing is if user click Never Ask Permission at any stage, App stop asking permission even after reinstall /fresh install after uninstalling. Is this the way it works or there is something I m missing. I also referred Android M - check runtime permission - how to determine if the user checked "Never ask again"? which nicely explain the things, but still want to clarify my confusion over it.

Here is code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView te1;

        te1 = (TextView) findViewById(R.id.te1);

        te1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {

                                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(MainActivity.this);
                                builder.setTitle("Title");
                                builder.setMessage("Fill Form")
                                        .setCancelable(false) //false will disable clicking anywhere in screen during alter.
                                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                                            }
                                        })
                                        .setNegativeButton("Do it Later", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();

                                            }
                                        });
                                android.app.AlertDialog alert = builder.create();
                                alert.show();

                    }else {
                        Toast.makeText(MainActivity.this, "You Selected Never Asked this Permission again", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Intent Fired", Toast.LENGTH_SHORT).show();
                }


            }
        });


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case (1):
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "App Need to access GPS to get your location which you can share", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this, "You did not get permission.Do Nothing", Toast.LENGTH_SHORT).show();
                }

        }


    }
}
Community
  • 1
  • 1
Panache
  • 1,701
  • 3
  • 19
  • 33

1 Answers1

0

AFAIK:

shouldShowRequestPermissionRationale()

This will return true if the app has requested the permission previously and the user denied the request - not if the user selected "never ask for this permission again".

The user will only see "never ask for this permission again" if they previously denied the request.

Do you still get a callback to onRequestPermissionsResult when you request the permission, even after pressing "never ask for this permission again"?

If so, you could listen for that callback and request for users to change the permissions directly via settings.

You can use the following method to direct the user to settings.

public static void goToAppSettings(@NonNull final Context context) {
    final Intent i = new Intent();
    i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    i.setData(Uri.parse("package:" + context.getPackageName()));
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    context.startActivity(i);
}
Charlie
  • 2,876
  • 19
  • 26
  • Two things, when user click never ask app toasts Toast.makeText(MainActivity.this, "You Selected Never Asked this Permission again", Toast.LENGTH_SHORT).show(); Perefetly Right. Problem is when I uninstall and reinstall app, I app still toast same message and do not ask permission – Panache Feb 13 '17 at 15:35
  • Changing position of shouldshow and your code for opening settings solved the issue. Thanks – Panache Feb 13 '17 at 15:43