我一直在开发一个发送短信的应用程序.我遇到的问题是sendTextMessage方法发送两条具有相同内容的消息.我该如何解决这个问题?
这个课开始这个过程
public class C2DMMessageReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { //Some stuff Log.i("C2DMMessageReceiver", "Got C2DM message"); SmsSend message = new SmsSend(context, phonenumber, line); message.send() }}
发送短信的班级
public class SmsSend { SmsSend(Context tcontext, String phoneNumber, String smstext){ context = tcontext; phone_number = phoneNumber; message = smstext; } protected voID send(){ if(foo){ Log.i("SmsSend", "Sending message"); SmsManager sms = SmsManager.getDefault(); String sent = "androID.telephony.SmsManager.STATUS_ON_ICC_SENT"; PendingIntent piSent = PendingIntent.getbroadcast(context, 0, new Intent(sent), 0); sms.sendTextMessage(phone_number, null, message, piSent, null); } }}
上课找出什么是幸福的
public class SmsSentbroadcastRecIEver extends broadcastReceiver{ private static final String TAG = "SmsSentbroadcastRecIEver"; @OverrIDe public voID onReceive(Context context, Intent intent) { switch (getResultCode()){ case Activity.RESulT_OK: Log.i(TAG,"SMS sent"); break; case SmsManager.RESulT_ERROR_GENERIC_FAILURE: Log.e(TAG,"Generic failure"); break; case SmsManager.RESulT_ERROR_NO_SERVICE: Log.e(TAG,"No service"); break; case SmsManager.RESulT_ERROR_NulL_PDU: Log.e(TAG,"PDU NulL"); break; case SmsManager.RESulT_ERROR_RAdio_OFF: Log.e(TAG,"Radio off"); break; } }}
LogCat的输出是
得到C2DM消息
发送消息
短信发送
短信发送
所以sendTextMessage只被触发一次,但它仍然会抛出两条消息.该怎么办?
我正在调试的设备是带有AndroID 4.0的三星galaxy S2.我读了一些旧的线程,sendTextMessage在某些(HTC)设备上被破坏,所以我尝试使用sendMultipartTextMessage,但它给出了相同的结果.
解决方法:
以下代码工作正常,S2与ICS:
voID sendMessageGTI9100ICS(String number, String msg) throws Exception { SmsManager m = SmsManager.getDefault(); Class aclass[] = new Class[9]; aclass[0] = String.class; aclass[1] = String.class; aclass[2] = ArrayList.class; aclass[3] = ArrayList.class; aclass[4] = ArrayList.class; aclass[5] = Boolean.TYPE; aclass[6] = Integer.TYPE; aclass[7] = Integer.TYPE; aclass[8] = Integer.TYPE; Method method = m.getClass().getmethod("sendMultipartTextMessage", aclass); Object aobj[] = new Object[9]; aobj[0] = number; aobj[1] = null; aobj[2] = m.divIDeMessage(msg); aobj[3] = null; aobj[4] = null; aobj[5] = Boolean.valueOf(false); aobj[6] = Integer.valueOf(0); aobj[7] = Integer.valueOf(0); aobj[8] = Integer.valueOf(0); method.invoke(m, aobj);}
总结 以上是内存溢出为你收集整理的Android sendTextMessage在exceution上发送两条完全相同的消息全部内容,希望文章能够帮你解决Android sendTextMessage在exceution上发送两条完全相同的消息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)