问题
I was trying to retrieve the phone number from intent extra through onrecieve() method for the following broadcast receiver registered in manifest file.
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
I can successfully able to read the phone number for all the version except android pie upgraded in my pixel 2 device. According to the documentation, app requires additional permission for reading phone number which are READ_CALL_LOG permission and the READ_PHONE_STATE permission but even after allowing these permission I still get missing permission for READ_CALL_LOG therefore I couldn't able to read the phone number. Please help me in resolving this issue.
回答1:
Make sure you grant READ_PHONE_STATE and READ_CALL_LOG permissions at runtime in Android 6.0+:
https://developer.android.com/distribute/best-practices/develop/runtime-permissions
Also, note that after granting these two permissions, you will receive ACTION_PHONE_STATE_CHANGED broadcast intent action twice; one with the EXTRA_INCOMING_NUMBER populated with the phone number, and another with it blank.
You can find more information here:
https://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED
回答2:
Per the docs the phone numbers given from the broadcast need extra permissions. because its like gaining access to call log info without requesting it.
Restricted access to phone numbers Apps running on Android 9 cannot read phone numbers or phone state without first acquiring the READ_CALL_LOG permission, in addition to the other permissions that your app's use cases require.
Phone numbers associated with incoming and outgoing calls are visible in the phone state broadcast, such as for incoming and outgoing calls and are accessible from the PhoneStateListener class. Without the READ_CALL_LOG permission, however, the phone number field that's provided in PHONE_STATE_CHANGED broadcasts and through PhoneStateListener is empty.
To read phone numbers from phone state, update your app to request the necessary permissions based on your use case:
To read numbers from the PHONE_STATE intent action, you need both the READ_CALL_LOG permission and the READ_PHONE_STATE permission. To read numbers from onCallStateChanged(), you need the READ_CALL_LOG permission only. You don't need the READ_PHONE_STATE permission.
ps. Please test this. In my Broadcastreciever i now need a awful block like this:
if(Build.VERSION.SDK_INT >= 26 && intent!=null && intent.getExtras() !=null
&& TextUtils.isEmpty(intent.getExtras().getString("incoming_number"))){
return;
}
回答3:
With the new Google Play permissions changes (i.e.post January 9, 2019 and Android Pie) none of the above works anymore.
You app will work locally or if the user downloads the APK manually but you can no longer update the app to the Play Store. Here's part of the email from Google Play.
"Hi Developers at Kishor Bapat9, Thanks for contacting the Google Play team about your app Ekalipi Call Announcer (ECA), com.ked.ekalipi.tts.
Publishing Status
Publishing status: Rejected
After review, your app has been rejected and wasn't published due to a policy violation. If you submitted an update, the previous version of your app is still available on Google Play.
Issue: Violation of Permissions policy After reviewing your app, we found that it doesn’t qualify to use the requested permissions for the following reason(s):
Based on our review, we found your app's expressed user experience did not match your declared core functionality {Caller ID, spam detection, and /or spam blocking}. Please remove these permissions from your app. "
Note- We have not specifically asked for these permissions. We have even tried changing the descriptions in different ways. CATCH 22 - If you specify the CALL_LOG permission Google Play rejects your app. If you remove the permission you cannot get the incoming call's number.
来源:https://stackoverflow.com/questions/51739395/read-numbers-from-the-phone-state-intent-action-android-9-not-working