在Android上发送短信
我做了一个java类来发送短信,但当我在同一个服务中多次调用该类时,我遇到了问题。
这些消息发送完美,但正如您在我的课程中所看到的,发送消息及其状态(无论是否成功或发生错误)时,我都有一个PendingIntent
侦听。
问题是:
如果我给同一个班级打两次电话,这两次BroadcastReceiver
收到相同的信息。
例如:如果我打电话给班级两次,而其中一次出货有错误。 两个BroadcastReceiver
收到相同的错误,或两者都收到,就好像它实际上已成功,但有一个错误。
我整个上午都在搜索,但还没有找到解决方案。 我希望有人知道我错在哪里:)
// imports.... public class SendSms { public SmsNoticeListener mListener; private UserGroupRead user; public interface SmsNoticeListener { public void onSentReceiver(UserGroupRead user, int resultcode); public void onDeliveredReceiver(UserGroupRead user, int resultcode); } private Context context; private String phoneNumber; private String message; private BroadcastReceiver sendBroadcastReceiver; private BroadcastReceiver deliveryBroadcastReceiver; public SendSms(Context context, SmsNoticeListener mListener, UserGroupRead user, String phoneNumber, String message) { this.context = context; this.mListener = mListener; this.user = user; this.phoneNumber = phoneNumber; this.message = message; } public void send() { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0); sendBroadcastReceiver = new BroadcastReceiver(){ public void onReceive(Context arg0, Intent arg1) { mListener.onSentReceiver(user, getResultCode()); } }; context.registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT)); deliveryBroadcastReceiver = new BroadcastReceiver(){ public void onReceive(Context arg0, Intent arg1) { mListener.onDeliveredReceiver(user, getResultCode()); } }; context.registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED)); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } public void close() { context.unregisterReceiver(sendBroadcastReceiver); context.unregisterReceiver(deliveryBroadcastReceiver); } }链接地址: http://www.djcxy.com/p/63375.html
上一篇: Send SMS on Android