开始活动

时间:2012-05-07 15:02:32

标签: android eclipse

当我试图在手机停止响铃时开始活动并且它一直强制关闭。

所以这是我的广播接收器

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;  
        }

      } 


    } 

如果您能告诉我如何在这种情况下获取上下文以启动Activity或其他方式如何启动它,我会很高兴。

谢谢。

1 个答案:

答案 0 :(得分:1)

广播接收器的上下文仅在onReceive呼叫期间有效:您无法保存并稍后重复使用。如果您需要以这种方式启动活动,那么您需要从Service的{​​{1}}方法启动BroadcastReceiver,并让该服务创建电话状态监听器并启动必需onReceive:一旦启动Activity,它就可以自行停止。