记录一个SmsManager发送短信的问题
2017-08-08 12:29
465 查看
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。
这样需要的所有方法和参数基本介绍完了,下边代码贴上:
首先注册广播
然后调用发送短信的方法
[code]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
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; } } }
然后调用发送短信的方法
[code]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
相关文章推荐
- 关于使用SMSManager发送短信字数限制问题及短信编码格式
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- thinkphp一个发送短信的接口问题
- iWebSMS2000短信发送问题记录
- android使用SmsManager发送长短信却接收到多条的问题(重新编辑)
- 一个需求规格说明书的问题记录
- 记录一个SPS2010中RSS Web Part报错的问题
- 当发现一个问题自己会了,但网上能找到答案的时候是记录还是不记录呢?
- 记录一个问题maven jar 引入
- 关于android4.4及以上版本无法恢复还原短信的问题记录与解决
- 用C#写了一个新增用户就发送邮件和手机短信的SqlServer触发器(附源码)
- 一个JS图片放大镜,实现动态记录的图片放大,避免内存泄露,解决IE6下无法遮盖select控件问题
- 利用java实现的一个发送手机短信的小例子
- 发送短信之分割短信 SMSManager
- 记录百度富文本编辑框 UEditor 的一个问题
- 长短信发送Emoji 图标被截断的问题分析
- 编写一个C#Windows服务程序,定时从数据库中拿出记录发送邮件。
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十五)Structured Streaming:同一个topic中包含一组数据的多个部分,按照key它们拼接为一条记录(以及遇到的问题)。
- 举例说明android中SmsManager的用法---用来实现手机发送短信的功能
- 一种解决在Linux环境下发送短信中文乱码问题的方案