如何检测来电

时间:2018-01-05 09:29:13

标签: android

我在文件清单中有代码:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".receiver.CallReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                </intent-filter>
        </receiver>
    </application>

</manifest>

并在broadcastReceiver中: 包com.ninhph.btvncallblock.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class CallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("call", "call coming: ");
    }
}

但是当我在模拟器中运行并调用时,没有任何反应,为什么?以及如何检测来电?

1 个答案:

答案 0 :(得分:0)

在AndroidManifest.xml中添加<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS"/>权限也尝试将priority设置为intent filter,如下面的代码

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".receiver.CallReceiver">
                <intent-filter android:priority="999">
                    <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                </intent-filter>
        </receiver>
    </application>

</manifest>
接收者代码中的

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class CallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("call", "call coming: ");
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, "Hey! Calling Number : " + incomingNumber, Toast.LENGTH_LONG).show();
              }
    }
}