'''编码如下,在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",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",int which) {
// Do something when user clicked the Yes button
activityflag.reject_call=true;
serverMain.getInstance().start();
}
});
builder.setNegativeButton("No",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,"Please turn on your location to use Helmet 2020",Toast.LENGTH_LONG).show();
finish();
}
});
builder.setNegativeButton("Cancel",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;
}
}