android中使用广播接收器的手机状态侦听器不工作
我正在尝试使用android中使用广播接收器的手机状态侦听器不工作,android,broadcastreceiver,android-logcat,phone-state-listener,Android,Broadcastreceiver,Android Logcat,Phone State Listener,我正在尝试使用广播侦听器检测传入呼叫。由于有人呼叫时我的应用程序中出现了一个故障,因此我的应用程序仍在播放歌曲。所以我想在来电时暂停这首歌。但我没有收到任何回应 这是完整的代码,这样你们都能明白我在搞什么 AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> } 在logcat中,不显示日志错误 那为什么这会让我有这样的行为呢。提前感谢尝试编辑onReceive(),如下所示 public void onReceive
广播侦听器检测传入呼叫。由于有人呼叫时我的应用程序中出现了一个故障,因此我的应用程序仍在播放歌曲。所以我想在来电时暂停这首歌。但我没有收到任何回应
这是完整的代码
,这样你们都能明白我在搞什么
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
}
在logcat
中,不显示日志错误
那为什么这会让我有这样的行为呢。提前感谢尝试编辑onReceive(),如下所示
public void onReceive(Context context, Intent intent)
{
TelephonyManager phoneManager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(phoneManager.getCallState() == TelephonyManager.CALL_STATE_RINGING)
{
string fromNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(TAG, "u have a call from a number " + fromNumber);
}
}
现在拨打您的号码,并在日志中查看您正在注册的监听器的号码。那是毫无意义的。作为
Android文档说明:
BroadcastReceiver对象仅在onReceive调用期间有效(上下文、意图)。一旦代码从此函数返回,系统将认为对象已完成且不再处于活动状态
在onReceive
中从Intent
获取EXTRA_STATE
,并在那里进行切换就足够了
如果您仍然没有得到任何记录,请将其调整到最小大小写-只在onReceive
中保留Log
语句。它有用吗?有两种情况下,应用程序无法接收广播-如果应用程序从未启动或被强制停止。日志cat中没有任何内容。尝试使用这两条语句,您将在onReceive()方法中获得一个日志删除所有内容。String newPhoneState=intent.getStringExtra(TelephonyManager.EXTRA_STATE)if(newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING){Log.i(“calling”,“u have a call”);}你找到答案了吗?你能回答这个问题吗?有办法解决这个问题吗
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());
}
}
}
public void onReceive(Context context, Intent intent)
{
TelephonyManager phoneManager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(phoneManager.getCallState() == TelephonyManager.CALL_STATE_RINGING)
{
string fromNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(TAG, "u have a call from a number " + fromNumber);
}
}