Android M permission request from Activity with noHistory = "true" and / or showOnLockScreen = "true"

I am working on a video calling application, and I have an “incoming call” screen that alerts the user when someone calls them. This screen is an action initiated by an incoming GCM and has the value noHistory = "true" and showOnLockScreen = "true" set in the manifest so that the user can make calls without unlocking their device.

If the user wants to accept the call, I run another action to participate in the actual call. However, before I run the second action, I will check for the necessary permissions (camera, microphone, etc.) and ask them if not.

This is where the problems arise.

Problem 1:

The access request dialog displayed by the system forces my activity to go to onPause. I believe, because under the hood this dialogue is actually an activity.

As a new activity begins here, using noHistory = "true" means that our activity is instantly killed. Technically, this is intentional behavior, and indeed, the Android team rejected this problem like this:

https://code.google.com/p/android-developer-preview/issues/detail?id=2915

I can get around this problem by manually managing it in onPause and detecting if there are any outstanding permission requests in flight, etc.

Problem 2: After working on problem 1, I get to stage 2.

, , .

, , , . .

: https://youtu.be/cobINQ9e2GY

, showOnLockScreen, true, , , .

, , "", ?

, . / .

, 2:

Activity, , ?

+4
1

requestPermission() (ActivityCompat):

, , . , , ​​ . , . onRequestPermissionsResult (int, String [], int []).

, , onPause() onResume() , - .

- :

private final int STATE_STARTING = 0;
private final int STATE_RUNNING = 1;
private final int STATE_REQUESTING_FINE_LOCATION_PERMISSION = 2;

private int state = STATE_STARTING;

@Override
public void onCreate() {
    super.onCreate();
    switch (state) {
        case STATE_STARTING:
            // do your initialization
            state = STATE_RUNNING;
            break;
    }
}

@Override
public void onResume() {
    super.onResume();
    switch (state) {
        case STATE_RUNNING:
            // handle other system events
            break;
        case STATE_REQUESTING_FINE_LOCATION_PERMISSION:
            // handle permission request event
            break;
    }
}

@Override
public void onPause() {
    super.onPause();
    switch (state) {
        case STATE_RUNNING:
            // handle other system events
            break;
        case STATE_REQUESTING_FINE_LOCATION_PERMISSION:
            // handle permission request event
            break;
    }
}

private void someFunction() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        state = STATE_REQUESTING_FINE_LOCATION_PERMISSION;
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION_FINE_LOCATION);
    } else {
        doProcessingRequiringFineLocationPermission();
    }

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_PERMISSION_FINE_LOCATION:
            if (grantResults != null && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                doProcessingRequiringFineLocationPermission();
            }
            state = STATE_RUNNING;
            break;
    }
}
+2

Source: https://habr.com/ru/post/1627001/


All Articles