代码之家  ›  专栏  ›  技术社区  ›  Entretoize

权限拒绝:需要android.Permission.READ_PHONE_STATE

  •  2
  • Entretoize  · 技术社区  · 6 年前

    我试图在我的android应用程序中检测电话呼叫,但在接到呼叫时收到以下消息:

    08-23 15:16:04.685  Vodafone VFD 600    Warning 850 BroadcastQueue  Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com....LogCalls requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)
    08-23 15:16:04.549  Vodafone VFD 600    Warning 850 BroadcastQueue  Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com....LogCalls requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)
    

    我的AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com...." android:installLocation="preferExternal">
        <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="27" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
      <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <application android:label="myapp" android:icon="@drawable/logo">
        </application>
    </manifest>
    

    我的广播接收器:

    [BroadcastReceiver]
    [IntentFilter(new[] {TelephonyManager.ActionPhoneStateChanged,Intent.ActionNewOutgoingCall })]
    public class LogCalls : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action == TelephonyManager.ActionPhoneStateChanged)
            {
                Console.WriteLine("state changed");
            }
        }
    }
    

    我错过了什么?

    1 回复  |  直到 6 年前
        1
  •  7
  •   Kevin Coppock    6 年前

    首先,不允许第三方应用程序获取 READ_PRIVILEGED_PHONE_STATE 许可。见 Privileged Permission Whitelisting :

    其次,当你的应用程序在API23及以上版本上运行时,你需要首先请求用户授予你 READ_PHONE_STATE Permissions Overview ).

    您需要按照 Request App Permissions 若要在运行时向用户请求权限,并且只有在授予该权限后 BroadcastReceiver 接受意愿。