电话状态广播接收器不起作用

时间:2018-06-23 13:37:39

标签: android broadcastreceiver phone-state-listener

我正在尝试获取来电结束后的呼叫者电话号码和呼叫日期/时间,然后开始活动并传递数据,但广播接收器不起作用。

我添加了一个代码以在控制台上打印一条消息,以检查是否调用了onReceived方法,但该方法从不打印。

我的AndroidManifest.xml代码:

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

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

    <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=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddDistressCallManagerActivity" />
        <activity android:name=".UpdateDistressCallManagerActivity" />
        <activity android:name=".AddDistressCallInfoActivity" />
        <activity android:name=".UpdateDistressCallInfoActivity" />
        <activity android:name=".DistressCallInfoListActivity" />
        <activity android:name=".DistressCallInfoActivity" />
        <activity android:name=".DistressCallManagersListActivity" />

        <receiver
            android:name=".PhoneStateBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我的PhoneStateBroadcastReceiver.java代码:

package com.inyavic.inyavicsage.distresscallsmanager;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "PhoneStateBroadcastReceiver";
    Context mContext;
    String incomingNum;
    private int prevState;


    @SuppressLint("LongLogTag")
    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        CustomPhoneStateListener customPhoneStateListener = new CustomPhoneStateListener();

        // register our listener with TelephonyManager
        telephony.listen(customPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        Bundle bundle = intent.getExtras();
        String phoneNum = bundle.getString("incoming_number");
        Log.v(TAG, "phoneNum" + phoneNum);
        mContext = context;

        System.out.println("RECEIVED");
    }

    public class CustomPhoneStateListener extends PhoneStateListener {

        private static final String TAG = "CustomPhoneStateListener";

        @SuppressLint("LongLogTag")
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (incomingNumber != null && incomingNumber.length() > 0)
                incomingNum = incomingNumber;

            // Answered call which is ended
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                prevState = state;
                Log.d(TAG, "CALL_STATE_OFFHOOK");
            }
        }
    }

}

可能是什么问题,解决方案是什么?

更新:

我尝试按照Jonas Lochmann的建议动态注册广播接收器,但仍然无法正常工作。

这是我添加到主要活动的onCreate方法中以动态注册广播接收器的代码:

PhoneStateBroadcastReceiver broadcastReceiver = new PhoneStateBroadcastReceiver();

this.registerReceiver(broadcastReceiver,
        new IntentFilter("android.intent.action.PHONE_STATE"));

0 个答案:

没有答案