3

I am working on Android Permission for M.

In my app in my activity I am starting a service, where I am using following code for Camera.

Code for Opening Camera in Service

cameraManager.openCamera(frontFacingCameraId, stateCallback, null);

I checked some documentations and i found that I cant request permission in service, I need to ask the permission in activity. So I am asking the permission in my activity.

Activity where requesting permission

if (PermissionUtil.getCameraPermission(this)) { Debug.d(TAG, "Camera Permission -- permission exsited"); performRecordingAction(); }

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case PermissionUtil.SDK__CAMERA_PERMISSION:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Debug.d(TAG, "Granted -- Camera Permission");
                performRecordingAction();
            } else {
                Debug.d(TAG, "Denied -- Camera Permission");
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            break;
    }
}

PermissionUtil

public static boolean getCameraPermission(Activity activity) {
    Debug.d(TAG, "getCameraPermission");
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) {
            Debug.d(TAG, "Already Denied -- Camera Permission");
            return false;
        } else {
            Debug.d(TAG, "Request Camera Permission");
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, SDK__CAMERA_PERMISSION);
            return false;
        }
    } else {
        return true;
    }
}

But in service at above line it is still throwing error for security exception.

Can anyone suggest where I am making mistake? And is there any way to request permission from service?

Vikash
  • 213
  • 5
  • 15

3 Answers3

2

You can not request permissions from a Service as you need an Activity context for the callback provided by the system. Also the permission request is running asynchronously from your app. See also the docs from requestPermissions() >>LINK:

This method may start an activity allowing the user to choose which permissions to grant and which to reject. Hence, you should be prepared that your activity may be paused and resumed. Further, granting some permissions may require a restart of you application. In such a case, the system will recreate the activity stack before delivering the result to onRequestPermissionsResult(int, String[], int[]).

A service can only check if he has the required permissions at runtime and then adapt his behavior.

Community
  • 1
  • 1
and_dev
  • 3,723
  • 1
  • 20
  • 28
0

Just add android:exported="true" in your manifest file as follows :

<activity
android:name="com.example.MainActivity"
android:label="Main" 
android:exported="true">
<intent-filter>
    <action android:name="android.intent.action.MAIN" >
    </action>
</intent-filter>

It will solve problem of Security Exception.

Is there any way to request permission from service?

Ya, follow Requesting permission from service

Community
  • 1
  • 1
user5716019
  • 598
  • 4
  • 17
0

As it is neccessary to check permissions inline in android SDK 23 (and later) you can ignore the shouldShowRequestPermissionRationale (which prompt user to set permission) and just check if permission was granted before:

public boolean checkPermission(){
    if (Build.VERSION.SDK_INT < 23) {
        return true;
    }
    else {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            //        android.Manifest.permission.CAMERA)) {
            //    PropmtForPermission();
            //} else {

                return false;
            //}
        }
        else{
            return true;
        }
    }
}
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82