我正在尝试为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);
}
我的主要活动
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();
}
});
答案 0 :(得分:0)
您在致电[3, 5, 7, 9]
[8, 12, 16]
[20, 28]
[48]
Result: 48
后设置了监听器。更改为以下内容:
sendMessage()
PS。您的OnSMSListener应为 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();
。