问题
I have Given this to Req Multiple permission at my splash screen
public class Main_MulPer extends Activity {
public static final int R_PERM = 321;
Context context = this;
public static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rcssa);
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.NFC,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
Main_MulPer.this.finish();
Intent ss = new Intent(Main_MulPer.this, Main_acti.class);
ss.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
ss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ss.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
ss.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(ss);
} else {
if (!hasPermissions(this, PERMISSIONS)) ;
{
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Main_MulPer.this.finish();
Intent i = new Intent(Main_MulPer.this, Splash_two.class);
startActivity(i);
}
}, 3000);
}
}
}
}
So here My problem is that Its Asking two Permission at a time...
If I try to grant them its moving to another Activity... So I have Given Similar 3 activities with 2 Permission on Each..
But Due to Handler its Opening new activities.. Then I removed Delay Handler... Now It opening Last activity... Directly...
Can Any one Suggest Me How to Start The Main Activity after All Permissions only... Without permission It should Exit app... Please Help me on this
Update
Insted of Multiple permissions I have Splited the 3 activities with two permission each... But here It should Go to next activity after permission But its Going to last activity every time first two activity permission are missing So I need to exit app and give them...
All I need is that without permission don't move to next screen
Can any one suggest me after permission only move to next activity....
回答1:
Try this,
public class Main_nPRC extends Activity {
public static final String MainPP_SP = "MainPP_data";
public static final int R_PERM = 2822;
private static final int REQUEST= 112;
Context mContext = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rcssa);
SharedPreferences settings = getSharedPreferences(MainPP_SP, 0);
HashMap<String, String> map = (HashMap<String, String>) settings.getAll();
if (Build.VERSION.SDK_INT >= 23) {
Log.d("TAG","@@@ IN IF Build.VERSION.SDK_INT >= 23");
String[] PERMISSIONS = {android.Manifest.permission.CAMERA,
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.INTERNET,
android.Manifest.permission.ACCESS_NETWORK_STATE,
android.Manifest.permission.ACCESS_WIFI_STATE,
android. Manifest.permission.NFC,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
if (!hasPermissions(mContext, PERMISSIONS)) {
Log.d("TAG","@@@ IN IF hasPermissions");
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
Log.d("TAG","@@@ IN ELSE hasPermissions");
callNextActivity();
}
} else {
Log.d("TAG","@@@ IN ELSE Build.VERSION.SDK_INT >= 23");
callNextActivity();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("TAG","@@@ PERMISSIONS grant");
callNextActivity();
} else {
Log.d("TAG","@@@ PERMISSIONS Denied");
Toast.makeText(mContext, "PERMISSIONS Denied", Toast.LENGTH_LONG).show();
}
}
}
}
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
public void callNextActivity()
{
Intent ss = new Intent(Main_nPRC.this, NMainSS.class);
ss.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
ss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ss.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
ss.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(ss);
finish();
}
@Override
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("ⓘ Exit ! " + getString(R.string.app_name));
alertDialogBuilder
.setMessage(Html.fromHtml("<p style='text-align:center;'>Please Fill the required details</p><h3 style='text-align:center;'>Click Yes to Exit !</h4>"))
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
回答2:
final int PERMISSION_REQUEST_CODE = 111;
if (Build.VERSION.SDK_INT >= 23) {
if (!checkReadContactPermission() ||!checkReadPhoneStatePermission()
|| !checkWriteExternalStorage() || !checkReadExternalStorage() ||
!checkSystemAlertWindowPermission() || !checkWriteContactPermission()) {
requestPermission();
} else {
// Move to main act
}
} else {
// Move to main act
}
You have to make method for check permission for each
for ex. this is for READ CONTACT, same way add all other
private boolean checkReadContactPermission() {
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
Method for request permissions
private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.READ_PHONE_STATE,
/* Manifest.permission.CAMERA,*/
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
finally RequestPermissionResult
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
// if granted then 0 else -1
// i have 5 permisson to check so 0,1,2,3,4..
if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED &&
grantResults[2] == PackageManager.PERMISSION_GRANTED &&
grantResults[3] == PackageManager.PERMISSION_GRANTED &&
grantResults[4] == PackageManager.PERMISSION_GRANTED) {
// means all permission are granted..move to Main activity
} else {
// show alert
}
break;
}
}
回答3:
When you request for permission(s) you'll receive results in onRequestPermissionsResult
handle results from inside as google docs says here
Updated
The permissions you mentioned are grouped ones example
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
it will ask only one permission for that and one will be for calls(Reading phone state) and you don't need to ask for permission for accessing network state and internet
来源:https://stackoverflow.com/questions/42686815/start-the-activity-after-multiple-permissionandroid-6