gpt4 book ai didi

android - 开始 Activity

转载 作者:行者123 更新时间:2023-11-30 04:13:13 26 4
gpt4 key购买 nike

当我尝试开始 Activity 时,手机停止响铃并且它一直强制关闭。

所以这是我的广播接收器

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

}

这应该开始 Activity

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

谢谢。

最佳答案

广播接收器的上下文仅在 onReceive 调用期间有效:您无法保存它并在以后重用它。如果您需要以这种方式启动 Activity,您需要的是从 BroadcastReceiveronReceive 方法启动一个 Service,并让该服务创建电话状态监听器并启动所需的 Activity:启动 Activity 后,它可以自行停止。

关于android - 开始 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com