'''编码如下,在Splash Activity中,我添加了权限,该权限将一再询问,直到授予权限为止,即使我授予了所有权限,也不会进入主活动。如果CheckPermission方法中的权限被拒绝,我该如何处理。而且我不想使用通常的启动屏幕方法,将屏幕启动几秒钟,我想留在该页面中直到所有权限都被授予。
public class SplashActivity extends AppCompatActivity{
private TextView tv;
private ImageView iv;
boolean haveConnectedWifi=false;
boolean haveConnectedMobile=false;
boolean gps_enabled=false;
boolean network_enabled=false;
myTravelAdapter mTA;
public static final int MULTIPLE_PERMISSIONS = 10; // code you want.
String[] permissions= new String[]{
Manifest.permission.CAMERA,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.SEND_SMS,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_activty);
CheckPermission();
haveNetworkConnection();
locationAlert();
NetworkAlert();
}
// FOR CHECKING PERMISSIONS
public boolean CheckPermission() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p:permissions) {
result = ContextCompat.checkSelfPermission(SplashActivity.this,p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),MULTIPLE_PERMISSIONS );
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissionsList[], int[] grantResults) {
switch (requestCode) {
case MULTIPLE_PERMISSIONS:{
if (grantResults.length > 0) {
String permissionsDenied = "";
for (String per : permissionsList) {
if(grantResults[0] == PackageManager.PERMISSION_DENIED){
permissionsDenied += "\n" + per;
}
}
// Show permissionsDenied
}
return;
}
}
}
private void updateView() {
}
public void NetworkAlert() {
if(!haveConnectedWifi && !haveConnectedMobile){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Please check your internet Connection.");
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
Toast.makeText(SplashActivity.this, "Please turn on your Wifi or Mobile Data to use Helmet 2020", Toast.LENGTH_SHORT).show();
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}else {
tv=(TextView)findViewById(R.id.tv);
iv=(ImageView)findViewById(R.id.iv);
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
// Set a title for alert dialog
builder.setTitle("");
// Ask the final question
builder.setMessage("Do you want to reject call wile driving?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when user clicked the Yes button
ActivityFlag.reject_call=true;
serverMain.getInstance().start();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when No button clicked
ActivityFlag.reject_call=false;
serverMain.getInstance().start();
if(CheckPermission()){
Intent i = new Intent (SplashActivity.this, MainActivity.class);
startActivity(i); finish();
}
else {
Toast.makeText(SplashActivity.this, "Please give all permissions", Toast.LENGTH_SHORT).show(); CheckPermission();
}
}
});
android.app.AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
}
}
//FOR CHECKING LOCATION ENABLED OR NOT AND WILL SHOW ALERT DIALOG
public void locationAlert() {
if( !gps_enabled && !network_enabled){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Please enable the location.");
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
Toast.makeText(SplashActivity.this, "Please turn on your location to use Helmet 2020", Toast.LENGTH_LONG).show();
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
//FOR CHECKING NETWORK STATE AND WILL SHOW ALERT DIALOG
public boolean haveNetworkConnection() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected()) {
haveConnectedMobile = true;
}
}
return haveConnectedWifi || haveConnectedMobile;
}
}
答案 0 :(得分:-1)
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for(int i = 0; i < permissions.length; ++i) {
String permission = permissions[i];
if (permission.equals("android.permission.READ_EXTERNAL_STORAGE") && grantResults[i] == -1) {
requestPermissions();
break;
}
}
}
public static void requestPermissions() {
List<String> permissions = checkPermissions();
if (permissions.size() > 0) {
ActivityCompat.requestPermissions(activity, (String[])permissions.toArray(new String[permissions.size()]), 0);
}
}
public static boolean isPermissionGranted(Activity ac) {
activity = ac;
List<String> permissions = checkPermissions();
if (permissions.size() == 0) {
return true;
} else {
requestPermissions();
return false;
}
}
public static List<String> checkPermissions() {
List<String> permissions = new ArrayList();
if (ContextCompat.checkSelfPermission(activity, "android.permission.READ_EXTERNAL_STORAGE") != 0) {
permissions.add("android.permission.READ_EXTERNAL_STORAGE");
permissions.add("android.permission.WRITE_EXTERNAL_STORAGE");
}
return permissions;
}
授予点击权限
isPermissionGranted(MainActivity.this);