1

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

所以这是我的广播接收器

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 或如何启动它,我会很高兴。

谢谢你。

4

1 回答 1

1

广播接收器的上下文仅在onReceive调用期间有效:您不能保存它并在以后重用它。如果您需要以这种方式启动 Activity,您需要ServiceBroadcastReceiver'onReceive方法启动 a,并让该服务创建电话状态侦听器并启动所需的 Activity Activity:一旦启动,Activity它就可以自行停止。

于 2012-05-07T15:27:30.477 回答