android - 在android中使用广播接收器的电话状态监听器不工作

标签 android broadcastreceiver android-logcat phone-state-listener

我正在尝试使用广播监听器检测来电。由于当有人调用时我的应用程序中收到一个错误,因此我的应用程序仍然播放这首歌。所以我想在来电期间暂停歌曲。但我没有收到任何回复。

这是完整的代码,以便大家都能明白我哪里搞砸了。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

IncomingCall.java

package com.example.suraj.freewee;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class IncomingCall extends BroadcastReceiver {

private String LOG_TAG = getClass().getSimpleName();
TelephonyManager telephonyManager;
Context context;

@Override
public void onReceive(Context context, Intent intent) {
    try {
        this.context = context;
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        Log.e(LOG_TAG, "inside on receive");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(LOG_TAG, e.toString());
    }


}

private class MyPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        Log.e(LOG_TAG, "Number : " + incomingNumber);
        try {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: {
                    //PAUSE
                    SongsAdapter.player.pause();
                    Log.e(getClass().getSimpleName(), "call ringing");
                    break;
                }
                case TelephonyManager.CALL_STATE_OFFHOOK: {
                    SongsAdapter.player.pause();
                    Log.e(getClass().getSimpleName(), "call offhook");
                    break;
                }
                case TelephonyManager.CALL_STATE_IDLE: {
                    //PLAY
                    SongsAdapter.player.start();
                    Log.e(getClass().getSimpleName(), "call idle");
                    break;
                }
                default: {
                }
            }
        } catch (Exception ex) {
            Log.e(getClass().getSimpleName(), "Error: " + ex.toString());
        }
    }
}

}

logcat中没有显示日志错误 那么为什么这会给我这样的行为。提前致谢

最佳答案

您正在 onReceive 中注册监听器。那是毫无意义的。作为 Android 文档指出:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

onReceive 中从 Intent 获取 EXTRA_STATE 并在那里进行switch 应该就足够了。

如果您仍然没有记录任何内容,请将其修剪为最小大小写 - 仅在 onReceive 中保留 Log 语句。有效吗? AFAIK 有两种情况应用程序不会接收广播 - 如果它从未启动或如果它被强制停止。

关于android - 在android中使用广播接收器的电话状态监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34740766/

相关文章:

android - BroadcastReceiver 尝试在无序广播期间返回结果

android - 如何使用 Logcat 打印非常大的字符串

java - EyeGesture和EyeGestureManager需要清晰

Android Studio Logcat 搜索

java - 如何使用关键字过滤 RSS 提要?

Android - 超链接不可点击

java - String.Format 中的阿拉伯字符 ("%d",1,Locale.US)

android - 如何在线程中重新加载 imageview?

android - BroadcastReceiver 不会对外部 Intent 使用react

Android:为什么系统重启后警报通知停止