我在MainActivity的onCreate中初始化接口SMSListener,但在重写的onReceive函数中它是null。我在onReceive中错误地使用了公共接口变量吗?每当我在重写函数之外测试它时,我已正确初始化变量。
SmsReceiver:
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();
}
}
}
MainActivity:
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)中的第一个测试在初始化接口变量后立即正常工作。当我在onReceive函数中尝试使用它时,我总是得到“SMSListener is NULL”调试消息。
答案 0 :(得分:1)
问题可能是粘性的broadcast
,它会在您onReceive
之前调用setCustomEventListener
方法,更好的方法是在构造函数中初始化它
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();
}
}
}
在你的活动中
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
中注册它,从清单中删除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());