回调监听器总是返回null
我正在尝试为Android创建一个简单的SMS库,并为其创建了一些自定义监听器。我想在事件发生时触发该监听器,例如。短信发送或失败。我能够发送和接收短信,但监听器事件没有被调用,因为我的监听器总是返回空值。有人可以告诉我我在哪里做错了吗?回调监听器总是返回null
public class SendSMS { private OnSMSListener listener;
public void OnSMSListener(OnSMSListener listener) {
this.listener = listener;
}
public SendSMS(Context context) {
Holder.setContext(context);
this.listener = null;
}
public void setNumber(String number) {
Holder.setMobNumber(number);
}
public void setMessage(String message) {
Holder.setMessage(message);
}
public void sendMessage() {
ImplSendSms.sendTextMessage(listener);
}
/**
* Interface definition for a callback to be invoked
*/
public interface OnSMSListener {
void onSmsSent();
void onGenericFailure();
void onNoService();
void onNullPdu();
void onRadioOff();
}
MyImplSendSms.class文件:
public class ImplSendSms { public static void sendTextMessage(final SendSMS.OnSMSListener listener) {
Toast.makeText(Holder.getContext(), "sendTextMessage", Toast.LENGTH_SHORT).show();
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(Holder.getContext(), 0,
new Intent(SENT), 0);
//---when the SMS has been sent---
Holder.getContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(Holder.getContext(), "Inside Sent", Toast.LENGTH_SHORT).show();
if (listener != null) {
Toast.makeText(Holder.getContext(), "Inside Sent Listener", Toast.LENGTH_SHORT).show();
listener.onSmsSent();
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
if (listener != null) {
listener.onGenericFailure();
}
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
if (listener != null) {
listener.onNoService();
}
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
if (listener != null) {
listener.onNullPdu();
}
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
if (listener != null) {
listener.onRadioOff();
}
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(Holder.getMobNumber(), null, Holder.getMessage(), sentPI, null);
}
我的MainActivity
SendSMS sms = new SendSMS(MainActivity.this); sms.setMessage("Hello");
sms.setNumber("123456789");
sms.sendMessage();
sms.OnSMSListener(new SendSMS.OnSMSListener() {
@Override
public void onSmsSent() {
Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show();
}
@Override
public void onGenericFailure() {
Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_SHORT).show();
}
@Override
public void onNoService() {
Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_SHORT).show();
}
@Override
public void onNullPdu() {
Toast.makeText(MainActivity.this, "Null PDU", Toast.LENGTH_SHORT).show();
}
@Override
public void onRadioOff() {
Toast.makeText(MainActivity.this, "Radio Off", Toast.LENGTH_SHORT).show();
}
});
回答:
要设置监听你叫sendMessage()
后。更改为以下内容:
sms.OnSMSListener(new SendSMS.OnSMSListener() { @Override
public void onSmsSent() {
Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show();
}
@Override
public void onGenericFailure() {
Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_SHORT).show();
}
@Override
public void onNoService() {
Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_SHORT).show();
}
@Override
public void onNullPdu() {
Toast.makeText(MainActivity.this, "Null PDU", Toast.LENGTH_SHORT).show();
}
@Override
public void onRadioOff() {
Toast.makeText(MainActivity.this, "Radio Off", Toast.LENGTH_SHORT).show();
}
});
sms.sendMessage();
PS。您的OnSMSListener应该是setOnSMSListener()
。
以上是 回调监听器总是返回null 的全部内容, 来源链接: utcz.com/qa/265115.html