android发送短信截获上一条发送是否成功,然后再来发送下一条短信

1.问题:在项目中遇到例如以下要求:待发短信有N条,实现一条一条的发送并在上一条短信发送成功之后再来发送下一条。

for(int i=0;i<3;i++){

  sendSMS(10086, text1, i);

  }



private void sendSMS(String toAddress, String body, Long id) {





// ---sends an SMS message to another device---

                     SmsManager sms = SmsManager.getDefault();

String SENT_SMS_ACTION = "SENT_SMS_ACTION";

// create the sentIntent parameter

Intent sentIntent = new Intent(SENT_SMS_ACTION);

sentIntent.putExtra("id", id);

   PendingIntent sentPI = PendingIntent.getBroadcast(

   ListOutgoingActivity.this, 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);

//你同一时候发送非常多信息的话,会产生非常多一样的PendingIntent,然后Android操作系统会把pendingIntent的数据更新到最新,所以toast的ID是最新的数据,曾经的数据会被覆盖掉。这个能够用来同步数据。

// 假设短信内容超过70个字符 将这条短信拆成多条短信发送出去

                    if (body.length() > 70) {

ArrayList<String> msgs = sms.divideMessage(body);

for (String msg : msgs) {

sms.sendTextMessage(toAddress, null, msg, sentPI,

null);

}

} else {

System.out.println("body====" + body);

sms.sendTextMessage(toAddress, null, body, sentPI, null);

}

BroadcastReceiver sendMessage = new BroadcastReceiver() {





@Override

public void onReceive(Context context, Intent intent) {

// 推断短信是否发送成功

switch (getResultCode()) {

case Activity.RESULT_OK:

Long id = intent.getLongExtra("id", -12);



                                                              //截取每次发送短信的ID,可是toast的id都是2???,正常情况下应该各自是0,1,2



Toast.makeText(ListOutgoingActivity.this,

id +"发送成功", Toast.LENGTH_SHORT).show();



break;

default:

Toast.makeText(ListOutgoingActivity.this,

"发送失败", Toast.LENGTH_LONG).show();

break;

}

}

};

registerReceiver(sendMessage, new IntentFilter(

SENT_SMS_ACTION));}

2.解决的方法:如今的解决方法是,收到上一条信息发送成功或者失败后,在发送下一条数据

int i=0;

sendSMS(10086,test, i) ;

private void sendSMS(String toAddress, String body, Long id) {





// ---sends an SMS message to another device---

                     SmsManager sms = SmsManager.getDefault();

 String SENT_SMS_ACTION = "SENT_SMS_ACTION";

// create the sentIntent parameter

Intent sentIntent = new Intent(SENT_SMS_ACTION);

sentIntent.putExtra("id", id);

    PendingIntent sentPI = PendingIntent.getBroadcast(

    ListOutgoingActivity.this, 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// 假设短信内容超过70个字符 将这条短信拆成多条短信发送出去

if (body.length() > 70) {

ArrayList<String> msgs = sms.divideMessage(body);

for (String msg : msgs) {

sms.sendTextMessage(toAddress, null, msg, sentPI,

null);

}

} else {

System.out.println("body====" + body);

sms.sendTextMessage(toAddress, null, body, sentPI, null);

}

BroadcastReceiver sendMessage = new BroadcastReceiver() {





@Override

public void onReceive(Context context, Intent intent) {

// 推断短信是否发送成功

switch (getResultCode()) {

case Activity.RESULT_OK:

Long id = intent.getLongExtra("id", -12);

                                    Toast.makeText(ListOutgoingActivity.this,id +"发送成功", Toast.LENGTH_SHORT).show();

i++;

if(i<3){

sendSMS(10086,test,i)

}

break;

default:

Toast.makeText(ListOutgoingActivity.this,

"发送失败", Toast.LENGTH_LONG).show();

break;

}

}

};

registerReceiver(sendMessage, new IntentFilter(

SENT_SMS_ACTION));}

android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信的更多相关文章

  1. Android下调用收发短信邮件等

    Android下调用收发短信邮件等 1,调web浏览器Uri myBlogUri = Uri.parse("http://xxxxx.com");returnIt = new In ...

  2. Android:webView加载h5网页视频,播放不了,以及横屏全屏的问题和实现自定义加载进度条的效果

    1.webView加载h5网页视频,播放不了,android3.0之后要在menifest添加硬件加速的属性 android:hardwareAccelerated="true". ...

  3. linux下进度条的编写和实现

    实现了一个简单的进度条,主要技术啥的算不上,但有几个需要注意的点 首先是回车符,回车符可不是\n,我们可以把\n看成是两个动作的合体,分别是,回车和换行,都有自己对应的符号,这利用回车符一直在同一个位 ...

  4. 自己制作一个链表用来存储列表信息,并查找当前id信息,找上一条信息,下一条信息(信息浏览的时候方便使用)

    偶然看到某些网站在新闻详情中 ,往往是需要根据当前信息id获取到上一条信息和下一条信息的,而通常我们的做法是先获取当前信息,再获取上一条信息,再获取下一条信息,就需要发送三次查询才能够得到这些信息,一 ...

  5. sql查询上一条记录和下一条记录

    上一条记录的SQL语句: * from news where newsid<id order by newsid DESC 下一条记录的SQL语句: * from news where news ...

  6. LINUX下PHP开启短标签short_open_tag支持

    LINUX下PHP开启短标签short_open_tag支持 以CENTOS为例: 找到php.ini #find / -name php.ini #/etc/php.ini 编辑php.ini #v ...

  7. asp.net 上一条和下一条记录的显示

    这里我用的是input标签跳转页面的: 前台aspx页面中: <input class="btn" id="btnSetForm" type=" ...

  8. android中自定义view---实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色

    android自定义view,实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色. 由于时间比较仓促,因此没有对代码进行过多的优化,功能远远不如androi ...

  9. 使用B或BL跳转时,下一条指令的地址是这样计算的

    B跳转指令:它是个相对跳转指令,其机器码格式如下: [31:28]位是条件码:[27:24]位为“1010”(0xeaffffff)时,表示B跳转指令,为“1011”时,表示BL跳转指令:[23:0] ...

随机推荐

  1. What is a good EPUB reader on Linux

    Last updated on August 20, 2014 Authored by Adrien Brochard 12 Comments If the habit on reading book ...

  2. HDU 1544 Palindromes(回文子串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...

  3. 分析php获取客户端ip

    用php能获取客户端ip,这个大家都知道,代码如下: /** * 获取客户端ip * @param number $type * @return string */ function getClien ...

  4. HDU - 2276 Kiki &amp; Little Kiki 2

    Description There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and ...

  5. 工具篇-TraceView

    --- layout: post title: 工具篇-TraceView  description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...

  6. Oracle如何实现跨数据库查询

    转发:http://www.linuxidc.com/Linux/2012-02/53974.htm 实现结果:在一个数据库中某个用户下编写一个存储过程,在存储过程中使用DBLINK连接另一个数据库, ...

  7. 抄书(B - 二分查找)

    抄书  (二分查找+贪心) 提示:二分查找一般写成非递归形式 时间复杂度:O(logn) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  8. 一个分组查询 每组前 10 的sql 语句

    USE tmpgo CREATE TABLE Employee( ID int identity(1,1), EmpName varchar(20), EmpSalary varchar(10), E ...

  9. Laravel 5.1 ACL权限控制 一

    请自行添加命名空间,代码下载地址 https://github.com/caoxt/learngit 1.所需要用到的数据表 users(用户表).roles(角色表).role_user(用户角色对 ...

  10. float与position

    使用float会使块级元素的宽高表现为包裹内容(在不设定宽高的情况下)  这是当然的  我们使用float就是使几个div排在一行 当然不可能在宽度上撑满父元素啦  至于高度 不论有没有float 高 ...