[英]Android broadcast receiver + static variable
I am working with a broadcast receiver and wondering how it works.我正在使用广播接收器并想知道它是如何工作的。 I have the following code to catch the state of a phone call:
我有以下代码来捕捉电话的状态:
private static String mLastState="Unknown last state";
private String phoneState="Unknown phone state";
private static boolean incomingCall=false;
@Override
public void onReceive(Context context, Intent intent) {
phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
LOG.debug("Outgoing call");
} else {
LOG.debug("Incoming call" + " " + String.valueOf(incomingCall));
if (!mLastState.equals(phoneState)) {
switch (phoneState) {
case("RINGING"):
incomingCall=true;
mLastState="RINGING";
LOG.debug("RINGING");
break;
case("OFFHOOK"):
if (incomingCall) {
mLastState = "OFFHOOK";
LOG.debug("OFFHOOK");
}
break;
case("IDLE"):
if (incomingCall) {
mLastState="IDLE";
incomingCall=false;
LOG.debug("IDLE");
}
break;
}
}
}
}
Incoming call logs:来电记录:
main: [Incoming call false]
main: [RINGING]
main: [Incoming call true]
main: [OFFHOOK]
main: [Incoming call true]
main: [IDLE]
main: [Incoming call false]
The logs confused me (specifically, the state of the incoming call static variable).日志让我感到困惑(特别是来电静态变量的状态)。 When we have
[RINGING]
, Broadcast initializes all variables ( incomingCall = false
), and then executes the onReceive
method (we get incomingCall = true
).当我们有
[RINGING]
,Broadcast 会初始化所有变量( incomingCall = false
),然后执行onReceive
方法(我们得到incomingCall = true
)。 The second operation is triggered ( [OFFHOOK]
), but does not initialize static field ( incomingCall
would be false
), and the receiver takes the old value of the variable;所述第二操作被触发(
[OFFHOOK]
但不初始化静态字段( incomingCall
将是false
),并且接收器需要的变量的旧值; although between these events may take more time and the same thing happens with the third operation ( [IDLE]
).尽管这些事件之间可能需要更多时间,并且第三次操作(
[IDLE]
)也会发生同样的事情。
How does the receiver initialize variables?接收者如何初始化变量? What happens with static variables?
静态变量会发生什么? Does the receiver initialize variables every time or not?
接收者是否每次都初始化变量? How do I save the incoming call variable correctly -- maybe Shared Preferences?
如何正确保存来电变量——也许是共享首选项?
You can use PhoneStateListener as given below:您可以使用PhoneStateListener如下所示:
public class BroadCastReceiver extends BroadcastReceiver {
TelephonyManager telManager;
Context context;
PhoneListener pl;
@Override
public void onReceive(Context context, Intent intent) {
telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
this.context = context;
String action = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.d("Actions--------->", action);
pl=PhoneListener.getInstance();
telManager.listen(pl, PhoneStateListener.LISTEN_CALL_STATE);
}
}
public class PhoneListener extends PhoneStateListener {
private static PhoneListener phoneListenerInstance;
private PhoneListener()
{
}
public static PhoneListener getInstance()
{
if(phoneListenerInstance==null)
{
phoneListenerInstance=new PhoneListener();
}
return phoneListenerInstance;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
Log.d("State", "ringing");
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
Log.d("State", "offhook");
break;
}
case TelephonyManager.CALL_STATE_IDLE: {
Log.d("State", "idle");
break;
}
default: {
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.