无法使用BroadcastReceiver [Marshmallow]检测来电号码

时间:2016-08-10 05:04:00

标签: java android

每当我使用BroadcastReceiver拨打来电时,我都会尝试记录电话号码。但是,当我拨打我的号码时,我没有得到任何log。这是代码:

的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.broadcast_phone" >

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <receiver android:name=".MyPhoneReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" >
                </action>
            </intent-filter>
        </receiver>
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent("tuet");
        sendBroadcast(intent);
    }
}

MyPhoneReceiver.java

    public class MyPhoneReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null)
        {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);

            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String phoneNumber = extras
                        .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.e("DEBUG", phoneNumber);
            }
        }
    }
}

我正在使用SDK 23编译,目标也是Marshmallow,在这种情况下我应该使用运行时权限吗?或者我错过了什么?

修改-1

似乎这个问题只与Marshmallow以后有关,当我降级目标sdk版本时,这个工作正常:targetSdkVersion 22

那么,如何调整它以在SDK23 +之后工作?

4 个答案:

答案 0 :(得分:2)

根据onCallStateChanged() documentation

  来电电话号码。如果应用程序没有READ_PHONE_STATE权限,则会将空字符串作为参数传递。

根据dangerous permission

定位API 23或更高版本时,READ_PHONE_STATErequested at runtimeMarshmallow Behavior Changes必须为{{3}}

答案 1 :(得分:1)

这是一个许可问题。在Marshmallow中,如果您的应用针对SDK 23,则会在运行时处理权限。请参阅https://developer.android.com/training/permissions/requesting.html。您可以通过在系统设置中手动启用应用所需的所有权限来测试此功能,然后您的应用就可以正常运行。

答案 2 :(得分:0)

在onReceive方法中尝试使用此代码段:

@Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                Log.e("DEBUG", incomingNumber);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }

参考:https://stackoverflow.com/a/13154533/2724418

答案 3 :(得分:0)

试试这个:

在清单中:

<receiver android:name=".PhoneCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

在PhoneCallReceiver中:

public void onReceive(Context context, Intent intent) {
    try {
        if (intent.getAction().equals("android.intent.action.PHONE_STATE"))
        {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                 TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);




                Bundle bundle = intent.getExtras();
                String phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.d("INCOMING", phoneNumber);

            }
        }
    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }
}