我正在尝试使用Broadcast Listener
检测来电。由于当有人拨打电话时我的应用中收到一个错误,我的应用仍会播放该歌曲。所以我想在来电期间暂停一首歌。但我没有收到任何回复。
这是完整的code
所以你们都可以理解我在哪里弄乱。
的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
中,不显示任何日志错误
那么为什么这会给我这样的行为呢。提前谢谢
答案 0 :(得分:1)
您正在onReceive
内注册听众。那是毫无意义的。如
Android文档声明:
BroadcastReceiver对象仅在对onReceive(Context,Intent)的调用期间有效。一旦您的代码从此函数返回,系统会认为该对象已完成且不再有效。
从EXTRA_STATE
Intent
onReceive
获取switch
并在Log
执行onReceive
就足够了。
如果您仍然没有记录任何内容,请将其修改为最小的情况 - 仅在 // RxJava Observable
Observable.OnSubscribe<Object> onSubscribe = subscriber -> {
try {
// Do the query or long task...
subscriber.onNext(object);
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
};
// RxJava Observer
Subscriber<Object> subscriber = new Subscriber<Object>() {
@Override
public void onCompleted() {
// Handle the completion
}
@Override
public void onError(Throwable e) {
// Handle the error
}
@Override
public void onNext(Object result) {
// Handle the result
}
};
Observable.create(onSubscribe)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
内留下 rpt.SetParameterValue("@param_name", the_value)
语句。它有用吗? AFAIK有两种情况,即应用程序无法接收广播 - 如果它从未启动或是强制停止。
答案 1 :(得分:0)
尝试编辑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);
}
}
现在拨打你的号码并查看日志中的号码