slyzer
slyzer

Reputation: 215

Android - How to know that I lost Permission to check calls (on Marshmallow)

I have an app that makes calls to a list of specific numbers.

Here's my Receiver from the Manifest:

        <receiver android:name=".manager.sos.CallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

And with that I know exactly when a call is made and when it finishes. When one call finishes, I make a new call after my "onOutgoingCallEnded" method beeing called. The method "onOutgoingCallEnded", is called through my CallReceiver, based on TelephonyManager states: EXTRA_STATE_IDLE, EXTRA_STATE_OFFHOOK, EXTRA_STATE_RINGING... So if CallReceiver isn't called, I don't reach the "onOutgoingCallEnded" method

My problem begin with Android 6.0 (marshmallow), because, even if the user gives permission to CALLS, he can any time he wants, reverse it on the App Settings.

So while the call is being made, and the user denies permission, I don't see my "onOutgoingCallEnded" method being called, because I lost permission to check Calls State.

So How can I know that I lost Permission to check Calls?

Upvotes: 2

Views: 447

Answers (5)

Abhinav Das
Abhinav Das

Reputation: 606

Well, it seems you've answered your own question. The simplest way (as I see it) would be for you to check if your onOutgoingCallEnded() method is being called to know if you have lost permission to check call state.

Upvotes: 0

Neh
Neh

Reputation: 452

With Android 6.0 Marshmallow, Google introduced a new permission model. Rather than the user blindly accepting all permissions at install time, the user is now prompted to accept permissions as they become necessary during application use. To provide a graceful handling of permission removal scenario you should do the following :

  • Even if the permission has been previously granted it is necessary to check again to be sure that the user did not later revoke that permission.

Code

 private boolean hasPermission(String permission){

        if(checkForMarshMallow()){
            return(checkSelfPermission(permission)==PackageManager.PERMISSION_GRANTED);

        }

        return true;

    }

    boolean checkForMarshMallow(){

        return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);

    }

The above method uses the method checkSelfPermission(String perm). This method is present in the Activity class and is used to return an integer value of PERMISSION_GRANTED or PERMISSION_DENIED which you can make use of

Upvotes: 0

Smit Davda
Smit Davda

Reputation: 638

When a call would be initiated it may be possible that the activity's pause method will be called and when the user comes back to the application the activity's resume method will be called so you can check for the permission in onResume and if the user would have withdrawn the permission you would be able to come to know about it

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006549

So How can I know that I lost Permission to check Calls?

You don't, at least not immediately. Android will terminate your process when you lose permission. It will not call any methods on your app to tell you that you lost permission.

You will only find out when your code runs for other reasons (e.g., user taps on your launcher icon), where you can call checkSelfPermission() to see whether you still have permission or not.

Your scenario is uncommon, but it reflects a bit of a hole in the runtime permission system, IMHO. Ideally, you would find out that your receiver will no longer be receiving such broadcasts.

Upvotes: 1

panonski
panonski

Reputation: 555

If you set the targetSdkVersion to 23 or higher, you'll be automatically requested to preface any part of code that uses "dangerous" permissions with a permission check. That way, you will "know" if you have lost your permission, i.e. each time your app needs to perform something that requires a permission that had been denied, it can ask the user to re-enable it, stating the reason for it. You can read about it in more detail here: https://developer.android.com/training/permissions/requesting.html

Upvotes: 0

Related Questions