The code I have written should check the permissions in the OnStart
method and regardless of the outcome of whether the user gives access, the same two pieces of code should execute:
- Starting my service
- Auto-login the user if they have provided credentials before
If I retype my credentials and turn the app on and off then on again - then for around 2-5 minutes it will automatically log me in. If I have the app off for longer than that, the autologin will not work.
OnStart:
@Override
protected void onStart() {
super.onStart();
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissions.add(Manifest.permission.ACTIVITY_RECOGNITION);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
permissions.add(Manifest.permission.SCHEDULE_EXACT_ALARM);
checkPermission(permissions.toArray(new String[0]), INITIAL_REQUESTS_CODE);
}
checkPermission Function:
public void checkPermission(String[] permissions, int requestCode) {
// Checking if permission is not granted
boolean ungrantedPermissions = false;
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED) {
ungrantedPermissions = true;
break;
}
}
if (ungrantedPermissions)
ActivityCompat.requestPermissions(this, permissions, requestCode);
else{
startStepCounterService();
attemptLoginWithSavedCredentials();
}
}
onRequestPermissionResult:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
startStepCounterService();
attemptLoginWithSavedCredentials();
}
startStepCounterService Function:
private void startStepCounterService() {
if(!isMyServiceRunning(StepCounterService.class))
{
Intent newIntent = new Intent(this, StepCounterService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(newIntent);
} else {
startService(newIntent);
}
}
}
attemptLoginWithSavedCredentials Function:
private void attemptLoginWithSavedCredentials() {
String savedUsername = SaveSharedPreference.getUserName(this);
String savedPassword = SaveSharedPreference.getUserPassword(this);
if(!savedUsername.isEmpty() && !savedPassword.isEmpty())
{
LoadingExtensions.showLoadingIcon(loadingIndicator, rootView);
loginManager.login(savedUsername, savedPassword);
}
}
CodePudding user response:
After looking around, I found this: https://stackoverflow.com/questions/52997092/requestpermission-how-to-wait-until-granted#:~:text=Put your logic in its own function(s), outside of onCreate().
Its not the ideal solution, nor does it the answer the question as to why the follow up code wasn't working anyway but I also had code in OnResume. I just moved all that and the 2 pieces of code in question to a function and followed the answer. It works from my testing though its not an ideal solution.
This may be a useful resource for others experiencing a similar issue but need an alternative solution: https://github.com/fondesa/kpermissions/issues/119
CodePudding user response:
Try changing your checkPermission
method to like this
public void checkPermission(String[] permissions, int requestCode) {
// Checking if permission is not granted
boolean ungrantedPermissions = false;
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED) {
ungrantedPermissions = true;
break;
}
}
if (!ungrantedPermissions)
ActivityCompat.requestPermissions(this, permissions, requestCode);
else{
startStepCounterService();
attemptLoginWithSavedCredentials();
}
}