记录一个SmsManager发送短信的问题

Android调系统发送短信使用的是SmsManager,一条短信的字数有一个最大长度限制,如果超过的话就需要拆分。

SmsManager为我们提供的方法有:

SmsManager.getDefault()静态方法获取SmsManager的默认实例对象;

SmsManager.divideMessage(content)发送内容大于70时自动拆分;

SmsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)发送一条短信;

SmsManager.sendMultipartTextMessage(destinationAddress, scAddress, parts, sentIntents, deliveryIntents)发送多条短信,一般与sms.divideMessage(content)同时使用;


一般情况使用上述方法就足够了,发送多条时接收方接收到的是多条短信成一条短信的形式,其实发送方是发送出去了多条。


下边说一下参数,发一条和发多条都是需要5个参数,前两个相同:destinationAddress表示目的地址正常也就是接收方号码;scAddress服务中心地址,一般置为null表示使用默认。

后边三个参数对比着说:

text:表示发送一条短信的内容; 

parts:表示ArrayList<String> parts,是一个短信列表,里边是拆分的多条短信;

sentIntent:表示发送方发送消息成功或失败的广播,如果不需要可以置null;

sentIntents:表示ArrayList<PendingIntent> sentIntents,sentIntent的列表集合;

deliveryIntent;接收方接收消息成功或失败的广播,一般置null;

deliveryIntents:表示ArrayList<PendingIntent> deliveryIntents,deliveryIntent的列表集合,也是一般置null。


发送一条或多条短信的方法都需要PendingIntent,那么这个东西到底是什么呢?

public final class PendingIntent extends Object implements Parcelable
我们看到其实它继承了Object并且实现了 Parcelable接口
PendingIntent 用于描述Intent及其最终的行为。

获取PendingIntent 对象的方法有多种,这里只介绍发送短信所需要的一种,

可以通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver发送广播的PendingIntent对象

这样我们就可以通过BroadcastReceiver来获取发送结果给与用户提示和进行下一步操作。

requestCode一般置为0 就可以;

intent就是你注册发送广播的意图;

flags有多个常量这里列出来几个比较会用到的:

FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。

FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。

FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

这样需要的所有方法和参数基本介绍完了,下边代码贴上:

首先注册广播

public static final String SENT_SMS_ACTION = "SENT_SMS_ACTION";
	private SMSSendResultReceiver mSMSReceiver = new SMSSendResultReceiver();
	private IntentFilter mSMSResultFilter = new IntentFilter();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aboutour_recommend_to_friend);

		content = getIntent().getStringExtra("content");
		
		//注册广播
		mSMSResultFilter.addAction(SENT_SMS_ACTION);
		registerReceiver(mSMSReceiver, mSMSResultFilter);

		init();
		Config.setContext(this.getApplicationContext());
	}
	
	
	private ReminderDialog reminderDialog = null;
	class SMSSendResultReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			switch (getResultCode()) {
			case Activity.RESULT_OK:
				if (reminderDialog == null) {
					reminderDialog = new ReminderDialog(RecommendToFriendActivity.this);
					reminderDialog.setCancelable(false);
					reminderDialog.setMessage("短信发送成功");
					reminderDialog.getEnterButton().setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							reminderDialog.dismiss();
							Intent intent = new Intent(RecommendToFriendActivity.this, MainActivity.class);
							startActivity(intent);
						}
					});
					reminderDialog.show();
				}
				break;
			default:
				ToastManager.makeText(RecommendToFriendActivity.this, "短信发送失败!");
				break;
			}
		}
	}
然后调用发送短信的方法
SmsManager sms = SmsManager.getDefault();
		Intent sentIntent = new Intent(SENT_SMS_ACTION);
		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
		
		//发送两条
//		String[] texts = content.split("http");
//		String text = "";
//		for (int i = 0; i < texts.length; i++) {
//			text = texts[i];
//			if (i == 1) {
//				sms.sendTextMessage(telephone, null, "http" + text, sentPI, null);
//			} else {
//				sms.sendTextMessage(telephone, null, text, sentPI, null);
//			}
//		}
		
		//发送一条
		if (content.length() > 70) {  
		    ArrayList msgs = sms.divideMessage(content);  
		    ArrayList sentIntents =  new ArrayList();  
		    for(int i = 0;i
因为我要发送的内容为一段文字加一个地址,所以拆分成两条的时候就从http截取了一下。
这样正常来说功能已经完美实现了。

但是问题来了,其他Android测试机都没有异常,只有在OPPO R9m上不行。
问题描述,只有每次杀死进程重新打开后第一次发送短信是正常的,发送成功后再一次发送的时候如果使用sendTextMessage的话只能发送出去第一条,
如果使用sendMultipartTextMessage的话就都发送不出去了。
如果不杀死进程的话每次获取到的SmsManager应该都是同一个地址,不知是否跟它有关,我会继续探索下去,也希望知道原因的哪位大侠可以帮我解惑一下,谢谢!


                                  
 


猜你喜欢

转载自blog.csdn.net/cunjicsdn/article/details/76878529