我正在尝试使用广播监听器
检测来电。由于当有人调用时我的应用程序中收到一个错误,因此我的应用程序仍然播放这首歌。所以我想在来电期间暂停歌曲。但我没有收到任何回复。
这是完整的代码
,以便大家都能明白我哪里搞砸了。
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/