googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.86k stars 1.46k forks source link

系统设置里开启或关闭权限,再次回到activity或fragment 会出现空白页 #269

Closed appsming closed 5 years ago

appsming commented 5 years ago

Basic Information

Device type: 华为 OS version: android 7.0 EasyPermissions version: 0.1.0____

Describe the problem

系统设置里开启或关闭权限,再次回到activity或fragment 会出现空白页 What happened? What did you expect to happen?

Code and logs

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{

private TextView  mLocResult = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLocResult  = findViewById(R.id.tv_result);
    findViewById(R.id.btn_loc).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkPersition();
        }
    });
    checkPersition();
}

@Override
protected void onResume() {
    super.onResume();
    try {
        if (isLocationEnabled()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (haveLocPermission()) {

                    getLocation();
                }
            } else {

                getLocation();

            }

        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

private  AlertDialog dialog = null;
private boolean haveLocPermission(){
    return EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION);
}

public static final int RC_CAMERA_PERM = 60666;

private  void checkPersition(){
    if (isLocationEnabled()){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!haveLocPermission()){
                EasyPermissions.requestPermissions(
                        this,
                        getString(R.string.rationale_loc_tips),
                        RC_CAMERA_PERM,
                        Manifest.permission.ACCESS_FINE_LOCATION);

            }else {
                getLocation();
            }
        }else {

            getLocation();

        }
    }else {
        if (null == dialog) {
        AlertDialog.Builder    dialogB = new AlertDialog.Builder(this);
            dialogB.setCancelable(true);
            dialogB.setTitle("Tips");
            dialogB.setMessage(R.string.rational_gps_no_open);
            dialogB.setPositiveButton(R.string.rationale_have_no_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(intent, 8888);

                }
            });

            dialogB.setNegativeButton(R.string.rationale_have_no_cancle, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
           dialog = dialogB.create();
           dialog.show();
        }else {

            if (!dialog.isShowing()){
                dialog.show();
            }else {
                dialog.dismiss();
            }

        }

    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

public boolean isLocationEnabled() {
    try {
        int locationMode = 0;
        String locationProviders;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        } else {
            locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }
    }catch (Exception e){
        e.printStackTrace();
        return  false;
    }
}

@AfterPermissionGranted(RC_CAMERA_PERM)
private  void getLocation(){
    BDLocationUtils.locate(MainActivity.this, new BDLocationUtils.OnLocateCompletedListener() {
        @Override
        public void onLocateCompleted(double latitude, double longitude, final BDLocation location) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (null !=location) {
                        mLocResult.setText(location.getAddrStr() + "\r\n" + location.getTime());
                    }
                }
            });
        }
    });

}

@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {

}

@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
    if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
        new AppSettingsDialog.Builder(this).setTitle("Tips").setRationale(getString(R.string.rationale_have_no_loc)).setPositiveButton(getString(R.string.rationale_have_no_ok)).setNegativeButton(getString(R.string.rationale_have_no_cancle)).setRequestCode(9999).build().show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
          if (haveLocPermission()){
              getLocation();
          }
    }else if(requestCode == 9999){
        if (haveLocPermission()){
            getLocation();
        }
    } if(requestCode == 8888){
          if (isLocationEnabled()){
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                  if (!haveLocPermission()){
                      EasyPermissions.requestPermissions(
                              this,
                              getString(R.string.rationale_loc_tips),
                              RC_CAMERA_PERM,
                              Manifest.permission.ACCESS_FINE_LOCATION);

                  }else {
                      getLocation();
                  }
              }else {

                  getLocation();

              }

          }
    }
}

}

// TODO(you): show the code that produces the problem, // and any relevant logs.

samtstern commented 5 years ago

@appsming sorry but I can only accept / answer issues written in English as I don't speak any other languages. It's not clear from the code alone what the problem is here, so I have to close the issue.

Please try StackOverflow or another community which might have people who are better able to help you with this issue in another language,