gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:39:06 26 4
gpt4 key购买 nike

我正在尝试使用 Broadcast 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.

得到EXTRA_STATE应该足够了来自 IntentonReceive做你的switch那里。

如果您仍然没有得到任何记录,请将其修剪为最小的大小写 - 仅保留 Log声明里面onReceive .它有效吗?据我所知,在两种情况下应用无法接收广播 - 如果它从未启动或被强制停止。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com