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();
}
}
}
}