Blue Nite
Blue Nite

Reputation: 118

How to request a permission from a Service

I am trying to request a permission on my Service. Usually to request a permission I have to pass an Activity. My application does not have an Activity.

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {    
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    }
}

How can I do it from a Service?

Upvotes: 4

Views: 2464

Answers (1)

AlexTa
AlexTa

Reputation: 5251

Request for permission from a service has no sense. Take notice that you ask for permission to a user when application needs to access to some system functionality, so you must request user for permission in your activity and then start your service.

Upvotes: 1

Related Questions