PhoneStateListener无法使用广播接收器

时间:2016-02-14 17:48:54

标签: android broadcastreceiver phone-state-listener

我正在尝试使用PhoneStateListener测试Broadcast Receiver。但我无法这样做。接收器类中的日志语句也没有在Logcat中打印。

这是清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suraj.phonelistener">
    <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">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
          </activity>
            <receiver android:name=".PhoneStateBroadcastReceiver" android:enabled="true">
                <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE"/>
                </intent-filter>
            </receiver>
    </application>
</manifest>

这是PhoneStateBroadcastReceiver.java文件

package com.example.suraj.phonelistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("DEBUG","########");
        // above line is also not printing in Logcat

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
    }

    public class CustomPhoneStateListener extends PhoneStateListener {

        //private static final String TAG = "PhoneStateChanged";
        Context context; //Context to make Toast if required
        public CustomPhoneStateListener(Context context) {
            super();
            this.context = context;
        }

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    //when Idle i.e no call
                    Log.d("DEBUG","IDLE");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //when Off hook i.e in call
                    //Make intent and start your service here
                    Log.d("DEBUG","OFFHOOK");
                case TelephonyManager.CALL_STATE_RINGING:
                    //when Ringing
                    Log.d("DEBUG","RINGING");
                    break;
                default:
                    break;
            }
        }
    }
}

问题一定是什么?

1 个答案:

答案 0 :(得分:2)

这可能是因为您的应用程序目标API级别为23,即android M(6.0)。 用户权限在6.0中有所不同 您可以将API级别从23更改为任何更低版本或遵循android M.的规则 以下是适用于Android M权限的文章link