Reputation: 353
on my app i have implemented a splash activity that check if is first run app and if is true show a dialog message, this is the code:
public void onConnected(Bundle connectionHint) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
myLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
if (isFirstStart) {
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(splashscreen.this).create();
alertDialog.setTitle(getResources().getString(R.string.titolo_intro_sms));
alertDialog.setMessage(getResources().getString(R.string.intro_sms));
alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(splashscreen.this, MainActivity.class);
startActivity(i);
}
});
alertDialog.show();
SharedPreferences.Editor e = getPrefs.edit();
e.putBoolean("firstStart", false);
e.apply();
} else {
startApp();
}
}
When dialog are show if click ok i open MainActivity. Now after 'Ok' click into dialog befor to start MainActivity i would like to show a dialog request permission. I have create a abstract class for this and i call in this way:
requestAppPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION},
R.string.msg,REQUEST_PERMISSION);
Now i set this line code into OnCreate the permission request are show before the splash activity but if i set into onClick methos of 'ok' allert dialog is not show.
How i can show the permission request after click ok befor to start Main Activity? Any help is great Thanks
I have integrate my onRequestPermissionresult into abstract class in this way:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for(int permisson : grantResults) {
permissionCheck = permissionCheck + permisson;
}
if( (grantResults.length > 0) && PackageManager.PERMISSION_GRANTED == permissionCheck) {
onPermissionsGranted(requestCode);
Intent i = new Intent(this, MainActivity.class); //start activity
startActivity(i);
} else {
//Display message when contain some Dangerous permisson not accept
Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.setData(Uri.parse("package:" + getPackageName()));
i.addCategory(Intent.CATEGORY_DEFAULT);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(i);
}
}).show();
}
}
and the message permission are show after ok click but after the app close and not open MainActivity
i have change my splashcrenn and i have set all method about permsion into it, this is code:
public void onPermissionsGranted(int requestCode) {
}
public void requestAppPermissions(final String[]requestedPermissions, final int stringId, final int requestCode) {
mErrorString.put(requestCode, stringId);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
boolean showRequestPermissions = false;
for(String permission: requestedPermissions) {
permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
showRequestPermissions = showRequestPermissions || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
if (permissionCheck!=PackageManager.PERMISSION_GRANTED) {
if(showRequestPermissions) {
Snackbar.make(findViewById(android.R.id.content), stringId, Snackbar.LENGTH_INDEFINITE).setAction("GRANT", new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(splashscreen.this, requestedPermissions, requestCode);
}
}).show();
} else {
ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
}
} else {
onPermissionsGranted(requestCode);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
Intent i = new Intent(splashscreen.this, MainActivity.class); //start activity
splashscreen.this.startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
into dialog 'ok' click button i have set this:
alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(splashscreen.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
/*Intent i = new Intent(splashscreen.this, MainActivity.class);
startActivity(i);*/
}
});
alertDialog.show();
but when i click ok the app close and not open MainActivity
Upvotes: 2
Views: 7145
Reputation: 23881
try this:
show permission dialog in button click:
alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); //show reuest dialog
}
});
alertDialog.show();
Now catch the result in onRequestPermissionsResult()
and start your MainActivity
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
Intent i = new Intent(splashscreen.this, MainActivity.class); //start activity
startActivity(i);
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
in your abstract class pass your activity context and use it to start MainActivity
To pass context:
class YourAbstractClass{
// variable to hold context
private Context context;
//save the context recievied via constructor in a local variable
public YourAbstractClass(Context context){ //constructor
this.context=context;
}
}
In your splash Activity call your Abstract class like:
YourAbstractClass class = new YourAbstractClass(this); //pass context
Now use the context to startActivity
Intent myIntent = new Intent(context, MainActivity.class);
context.startActivity(myIntent);
Upvotes: 1
Reputation: 3372
Use if-else statement in your button call.
If you already have permission then execute the MainActivity call in if part. If not then ask for permission in else part of if-else.
Now use OnRequestPermissionResult method to which is called when permissions are granted or denied. If permissions are granted in this method then again execute the MainActivity call or else just do what you need to do when permission denied like show him the reason why you need permission and try again or just exit.
Hope it helped. For everything about permissions in android in a simplified version just click HERE (http://www.vogella.com/tutorials/AndroidPermissions/article.html).
Upvotes: 0
Reputation: 2826
Request the permission after you click ok, but don't start the activity right away. Wait for the result in onRequestPermissionResult, and start your MainActivity
there.
Upvotes: 0