我知道这是一个问题。通过使用广播接收器并在接收器标签中使用android.intent.action.PHONE_STATE追踪来电动作
,您可以知道手机的操作。但是,我怎样才能确定它是来电还是来电?
这里是我的代码
@Override
public void onReceive(Context context, Intent intent)
{
this.context = context ;
System.out.println(":::called onReceiver:::");
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneCallListener = new PhoneStateListener()
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
isRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (isRinging)
{
hasAttended = true ;
isRinging = false;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (hasAttended)
{
isCallEnded = true ;
hasAttended = false;
}
break;
}
if(isCallEnded)
{
isCallEnded=false;
Intent callIntent = new Intent(context.getApplicationContext(),MyActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
super.onCallStateChanged(state, incomingNumber);
}
};
这里每一个phoneCallListener对象被创建并增加了通过每次一onCallStateChanged呼叫率的时间..
@巨力,你有没有解决这个问题..? –
@NilayOnAndroid是的...代码工作..但我创建了phoneStateListener一个单独的类来获得同样的实例,并设定布尔标志 – arul