2012-05-23 51 views
1

我知道这是一个问题。通过使用广播接收器并在接收器标签中使用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呼叫率的时间..

+0

@巨力,你有没有解决这个问题..? –

+0

@NilayOnAndroid是的...代码工作..但我创建了phoneStateListener一个单独的类来获得同样的实例,并设定布尔标志 – arul

回答

1

This是同时使用广播接收器一个方便的教程并使用android.intent.action.PHONE_STATE根据调用状态创建不同的Toast。当你得到这个工作,你将能够操纵的代码做你想要什么,当一个呼叫传入或传出

首先你的类必须extend BroadcastReceiver

接下来,您必须创建一个会听的方法手机状态... PhoneStateListener将听的时候,手机状态改变

然后做一个简单的switch case取决于它是否是一个呼入或呼出

编辑

赶通话

您只需编写一些代码来检查以前的状态是否“响起”。如果当前状态空闲并且之前的状态正在响铃,则他们取消/错过了呼叫。如果当前状态摘机并且之前的状态正在响铃,他们应答了该呼叫。

switch(state){ 
     case TelephonyManager.CALL_STATE_RINGING: 
      Log.i(LOG_TAG, "RINGING"); 
      wasRinging = true; 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 
      Log.i(LOG_TAG, "OFFHOOK"); 

      if (!wasRinging) { 
       // Start your new activity 
      } else { 
       // Cancel your old activity 
      } 

      // this should be the last piece of code before the break 
      wasRinging = true; 
      break; 
     case TelephonyManager.CALL_STATE_IDLE: 
      Log.i(LOG_TAG, "IDLE"); 
      // this should be the last piece of code before the break 
      wasRinging = true; 
      break; 
    } 
+0

hi..i特别希望在什么情况下你可以说这是一个来电(不误如果状态= TelephonyManager.CALL_STATE_RINGING,则呼叫接入,但是如何确定呼叫是否出席或未接通。余米,旨在根据结果显示弹出式 – arul

+0

我有更多的代码添加到它应该检查是否呼叫被应答或错过了...你可以举杯简单地添加到代码,这将取决于弹出的答案是什么发生并把什么信息到你想要的烤面包 –

+0

谢谢..我工作..我有覆盖onCallStateChanged()。但这种方法多次被调用...(近9次)。你能解释为什么吗? – arul