gpt4 book ai didi

java - 重写 OnReceive 中的 Null 局部变量

转载 作者:行者123 更新时间:2023-12-02 11:58:49 24 4
gpt4 key购买 nike

我在 MainActivity 的 onCreate 中初始化了接口(interface) SMSListener,但在重写的 onReceive 函数中它为 null。我是否在 onReceive 中错误地使用了公共(public)接口(interface)变量?每当我在重写函数之外测试变量时,我都会正确初始化该变量。

短信接收器:

public class SmsReceiver extends BroadcastReceiver {
public interface OnCustomReceiveSMS {
void OnReceiveSMS(String inNumber, String inText);
}
public OnCustomReceiveSMS SMSListener;

//constructor
public SmsReceiver(){
this.SMSListener = null;
}

//register sms listener
public void SetCustomEventListener(OnCustomReceiveSMS eventListener){
this.SMSListener = eventListener;
this.SMSListener.OnReceiveSMS("test1", "test2");
}

@Override
public void onReceive(Context context, Intent intent) {
//THIS IS ALWAYS FALSE EVEN AFTER INITIALIZED
if(this.SMSListener != null){
Toast.makeText(context, "SMSListener is NOT null", Toast.LENGTH_SHORT).show();
this.SMSListener.OnReceiveSMS("test3", "test4");
}else{
Toast.makeText(context, "SMSListener is NULL", Toast.LENGTH_SHORT).show();
}
}
}

主要 Activity :

public class MainActivity extends AppCompatActivity {
SmsReceiver smsReceiverObj;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//initialize SMS receiver and register event listener
this.smsReceiverObj = new SmsReceiver();
this.smsReceiverObj.SetCustomEventListener(new SmsReceiver.OnCustomReceiveSMS() {
@Override
public void OnReceiveSMS(String inNumber, String inText) {
Toast.makeText(getApplicationContext(), "test event listener fired", Toast.LENGTH_SHORT).show();
}
});
}
}

SetCustomEventListener 中的第一个测试(test1,test2)在初始化接口(interface)变量后立即正常工作。当我稍后尝试在 onReceive 函数中使用它时,我总是收到“SMSListener is NULL”调试消息。

最佳答案

问题可能是粘性广播,它会在setCustomEventListener之前调用onReceive方法,更好的方法是初始化它在构造函数中

public class SmsReceiver extends BroadcastReceiver {
public interface OnCustomReceiveSMS {
void OnReceiveSMS(String inNumber, String inText);
}
public OnCustomReceiveSMS SMSListener;

//constructor
public SmsReceiver(OnCustomReceiveSMS eventListener){
this.SMSListener = eventListener;
this.SMSListener.OnReceiveSMS("test1", "test2");
}

@Override
public void onReceive(Context context, Intent intent) {
//THIS IS ALWAYS FALSE EVEN AFTER INITIALIZED
if(this.SMSListener != null){
Toast.makeText(context, "SMSListener is NOT null", Toast.LENGTH_SHORT).show();
this.SMSListener.OnReceiveSMS("test3", "test4");
}else{
Toast.makeText(context, "SMSListener is NULL", Toast.LENGTH_SHORT).show();
}
}
}

在您的 Activity 中

this.smsReceiverObj = new SmsReceiver(new SmsReceiver.OnCustomReceiveSMS() {
@Override
public void OnReceiveSMS(String inNumber, String inText) {
Toast.makeText(getApplicationContext(), "test event listener fired", Toast.LENGTH_SHORT).show();
}
});

编辑

问题是您尝试在 Manifest 中注册它,从 list 中删除 receiver 并将其注册到 MainActivity 中,它的工作方式如下一个魅力

this.smsReceiverObj = new SmsReceiver(new SmsReceiver.OnCustomReceiveSMS() {
@Override
public void OnReceiveSMS(String inNumber, String inText) {
Toast.makeText(getApplicationContext(), "test event listener fired", Toast.LENGTH_SHORT).show();
}
});
registerReceiver(this.smsReceiverObj,new IntentFilter());

关于java - 重写 OnReceive 中的 Null 局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406401/

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