2012-05-07 204 views
1

當我嘗試開始活動時,電話停止振鈴,並強制關閉所有的時間。開始活動

所以這裏是我的廣播reciever

public class Incoming extends BroadcastReceiver { 


    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     // TODO Auto-generated method stub 

     MyPhoneStateListener phoneListener=new MyPhoneStateListener(); 
      TelephonyManager telephony = (TelephonyManager) 
      arg0.getSystemService(Context.TELEPHONY_SERVICE); 
     phoneListener.context = arg0; 
      telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE); 
    } 

} 

,這應該開始活動

public class MyPhoneStateListener extends PhoneStateListener { 

    int prevstate; 


    public void onCallStateChanged(int state,String incomingNumber){ 


     if(prevstate==TelephonyManager.CALL_STATE_OFFHOOK && state == TelephonyManager.CALL_STATE_IDLE){ 

       Log.i("PARm","Here the activity should start"); 
      } 
     else if(prevstate==TelephonyManager.CALL_STATE_RINGING && state == TelephonyManager.CALL_STATE_IDLE){ 

      Log.i("PARM","Here the activity should start"); 

     } 



    switch(state){ 
     case TelephonyManager.CALL_STATE_IDLE: 
      Log.d("DECallNoteActivity.get getContext();BUG", "IDLE"); 
      prevstate = state; 
     break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 
      Log.d("DEBUG", "OFFHOOK"); 
      prevstate = state; 
     break; 
     case TelephonyManager.CALL_STATE_RINGING: 
      Log.d("DEBUG", "RINGING"); 
      prevstate = state; 
     break; 
     } 

     } 


    } 

我會很高興,如果你能告訴我怎麼去語境在這種情況下,開始活動或另一種方式如何啓動它。

謝謝。

+0

請發佈任何logcat錯誤。 – Sam

+0

您的BroadcastReceiver如何設置?在清單中使用標記或使用Context.registerReceiver()? –

回答

1

廣播接收機的上下文僅在onReceive呼叫期間有效:您不能保存並稍後重新使用它。如果您需要以這種方式啓動活動,您需要的方法是從BroadcastReceiveronReceive方法開始Service,並讓該服務創建電話狀態偵聽器並啓動所需的Activity:啓動Activity後,它可以停止自己。

+0

**謝謝,你讓我的一天** – J1and1