我正在尝试使用BroadcastReceiver来接收来电。我希望在通话结束时注册通话。下面的代码使用相同的数字(不是过去调用的问题)给出了每个状态(空闲,响铃n摘机)不止一次。是因为在服务中调用了phonestatelistner。
public class IncomingCallReceiver extends BroadcastReceiver {
int stateString =0;
int prevStat = 0;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String p = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
prevStat = stateString;
stateString = 1;
Log.i("ON CALL STATE CHANGED","IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("ON CALL STATE CHANGED","OFFFHOOK");
prevStat = stateString;
stateString = 2;
break;
case TelephonyManager.CALL_STATE_RINGING:
prevStat = stateString;
stateString = 3;
Log.i("ON CALL STATE CHANGED","RINGING");
break;
}
if(prevStat == 2 && stateString == 1)
{
Log.i("REGISTER A CALLL","==" +p);
}
}
};
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
我也尝试使用phonestatelistener在一个单独的类中做同样的事情,但它没有被调用,即我的代码没有到达那里。在我的清单文件中,我已注册接收器,但没有phonestatelistener的声明。这是要注册吗?
我很抱歉,如果解决方案很明显,但对于android事物来说是新手。 任何帮助表示赞赏:)
答案 0 :(得分:0)
是,选择听众或收到,而不是两者