Reputation: 167
I am asking for run time permission. It is asking for the permission and everything is good. But the problem is that the permission dialog is somehow showing outside of the Activity. It close the Activity and then show the dialog of permission. I want that dialog to be show in my activity.
This is my Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
setPasswordText = (EditText) findViewById(R.id.setPasswordText);
setSecurityText = (EditText) findViewById(R.id.setSecurityText);
messageToSendText = (EditText) findViewById(R.id.messageToSendText);
numberToSendText = (EditText) findViewById(R.id.numberToSendText);
/* if(Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1234);
}
}
else
{
Intent intent = new Intent(this, Service.class);
startService(intent);
} */
KING();
}
// @TargetApi(Build.VERSION_CODES.M)
public void KING() {
// Toast.makeText(this, "Executed", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= 23){
if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
Toast.makeText(this, "RECEIVE SMS permission is needed", Toast.LENGTH_LONG).show();
}
// Toast.makeText(this, "Permission Not Granted", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, REQUEST_SEND_SMS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
if(requestCode == REQUEST_SEND_SMS){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "PERMISSION GRANTED BY YOU", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "PERMISSION DENIED BY YOU", Toast.LENGTH_LONG).show();
}
}else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Upvotes: 2
Views: 2301
Reputation: 2265
OK, I seem to have found a fix...
Overriding the onRequestPersmissionsResult
function solves both of my problems.
First of all the dialog now appears in front of my activity screen.
Secondly, I am now able to control what happens based on whether the user grants or denies the requested permission.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
if(requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
// Toast.makeText(this, "PERMISSION GRANTED BY YOU", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Opening app without location permission...", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(this, websiteViewActivity.class);
startActivity(intent);
finish();
}else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
I'll need to tweak the logic a bit as I add the extra permissions, but this has got me a good starting point and solves the two immediate problems.
Upvotes: 0
Reputation: 2475
I do not know if this solves your problem but it solved mine. For historical reasons which I had long forgotten I had the following flag set on my activity
Intent.FLAG_ACTIVITY_NO_HISTORY
This means that as soon as the activity is not in the foreground it closes. So when the permission dialog popped up, my activity went down. The answer was to get rid of that flag. It only took me two days of frustration to figure that out.
Upvotes: 1