我正在尝试BroadcastReceiver
使用READ_PHONE_STATE
权限。我要求用户获得运行权限(Android M),但毕竟我得到了Permission Denial:
W / BroadcastQueue:权限拒绝:接收Intent { act = android.intent.action.PHONE_STATE flg = 0x10(有额外的)}到 com.pb.qostest / .network.PhoneStateBroadcastReceiver需要 android.permission.READ_PRIVILEGED_PHONE_STATE由于发件人android (uid 1000)
对于权限,我使用的是此链接中的代码:https://stackoverflow.com/a/38764861
到目前为止,我希望我的手机状态BroadcastReceiver
只打印一些内容,如下所示:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("RECEIEVER", "PhoneStateBroadcastReceiver Receiver started!");
}
}
无论如何,由于上述许可拒绝,不会打印任何内容。
明显是:
<receiver
android:name=".network.PhoneStateBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
...
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
任何想法应该看起来如何相似或者问题是什么?
答案 0 :(得分:1)
为了能够检测到电话,您需要:
在Manifest.xml中1,定义您的接收者并要求PHONE_STATE权限:
<receiver android:name=".network.PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
// ...
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
2添加一个类PhoneStateBroadcastReceiver,它扩展BroadcastReceiver并覆盖onReceive()
3在运行时,request PHONE_STATE权限预先
ActivityCompat.requestPermissions(myMainActivity,
new String[]{Manifest.permission.READ_PHONE_STATE},
READ_PHONE_STATE_CODE);
并通过系统对话框
给出4拨打电话
你会看到onReceive()中的意图:
intent: Intent { act=android.intent.action.READ_PHONE_STATE flg=0x10 cmp=com.myApp.network.PhoneStateBroadcastReceiver (has extras) }
Action: android.intent.action.PHONE_STATE
希望有所帮助