android发送短信并监听插入收件箱的方法
2015-09-15 18:29
405 查看
package com.sms.service; import android.app.Activity; import android.app.PendingIntent; import android.content.*; import android.net.Uri; import android.telephony.SmsManager; import org.apache.commons.lang3.RandomUtils; import java.util.List; /** * Created by Administrator on 2015/8/18. */ public class SmsService { //移动,每小时70,一天500 // //电信 每小时200,一天1000 private static final SmsService instance = new SmsService(); public static final SmsService getInstance() { return instance; } public int send(final String phone, String message, final Context context) { if (message != null && phone != null) { SmsManager sms = SmsManager.getDefault(); final List<String> texts = sms.divideMessage(message); PendingIntent sendIntent = null; PendingIntent backIntent = null; //处理返回的发送状态 String SENT_SMS_ACTION = "SENT_SMS_ACTION"; Intent sentIntent = new Intent(SENT_SMS_ACTION); for(int i=1;i<texts.size()+1;i++) { try { sentIntent.putExtra(i + "", texts.get(i - 1)); }catch (Exception e){ e.printStackTrace(); } } sendIntent = PendingIntent.getBroadcast(context, phone.hashCode(), sentIntent, 0); //下标 final StringBuffer index = new StringBuffer("0"); context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent intent) { index.append(Integer.parseInt(index.substring(index.length() - 1))+1); switch (getResultCode()) { case Activity.RESULT_OK: String text = intent.getStringExtra(index.substring(index.length() - 1)); if (text == null){ break; } ContentValues values = new ContentValues(); values.put("date", System.currentTimeMillis()); values.put("read", 0); values.put("type", 2); values.put("address", phone); values.put("body", text); System.out.println("text:"+text); context.getContentResolver().insert(Uri.parse("content://sms/sent"), values); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } if (Integer.parseInt(index.substring(index.length() - 1)) == texts.size()){ context.unregisterReceiver(this); } } }, new IntentFilter(SENT_SMS_ACTION)); for(int i=0;i<texts.size();i++) { try { //发送短信 sms.sendTextMessage(phone, null, texts.get(i), sendIntent, backIntent); Thread.sleep(RandomUtils.nextLong(100, 600)); } catch (Exception e) { e.printStackTrace(); } } return texts.size(); } return 0; } }
相关文章推荐
- Android Ion 框架 文件下载
- Android画图Path的使用
- 一篇详细的 Android onTouch事件传递机制
- Android 百度地图 缩放级别 自适应
- Android性能优化总结
- emoji表情android,ios适配转换(一)
- Android笔记(二十九) Android中的异步更新(一) Android中的线程
- android Listview,gridview局部刷新,部分刷新
- Android动态设定GridView的高度,固定column,实现高度自适应
- Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
- android toast设置比Toast.LENGTH_SHORT还短的持续时间
- android技术内幕心得
- Android之Animation动画的介绍及用法
- android ndk开发
- Android沉浸模式,沉浸式顶栏,变色龙状态栏
- Android多行文本折叠展开效果
- android懒人笔记
- android获取已安装nolaunch(没有安装图标)的apk
- Android之matrix类控制图片的旋转、缩放、移动
- android【采用pull解析xml文件】