Android 监听短信内容变化,并发送到自己的手机
2016-04-29 21:10
891 查看
说明:主要用到了getContentResolver的内容监听
关于public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)说明
Parameters
destinationAddress 发送短信的地址(也就是号码)
scAddress 短信服务中心,如果为null,就是用当前默认的短信服务中心
text 短信内容
sentIntent 如果不为null,当短信发送成功或者失败时,这个PendingIntent会被广播出去成功的结果代码是Activity.RESULT_OK,或者下面这些错误之一 :RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
对于 RESULT_ERROR_GENERIC_FAILURE, the这个sentIntent可能包括额外的”errorCode”,包含一些具体有用的信息帮助检查 。基于SMS控制的全部程序检查 sentIntent. 如果 sentIntent 为空,the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.
deliveryIntent 如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播,状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据(”pdu”)
public class ContentObserverActivity extends BaseActivity { private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_observer); } /** * 拦截短信 * @param view */ public void cutSms(View view){ uri = Uri.parse("content://sms"); getContentResolver().registerContentObserver(uri, true, new MyContentObserver(new Handler())); } private class MyContentObserver extends ContentObserver{ public MyContentObserver(Handler handler) { super(handler); show("短信变动了"); //查询最后一条短信 Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToLast(); String body = cursor.getString(cursor.getColumnIndex("body")); SmsManager sm = SmsManager.getDefault(); ArrayList<String> list = sm.divideMessage(body); for (String msg : list) { sm.sendTextMessage("5556", null, msg, null, null); } } } public void show(String text){ Toast.makeText(this, text, Toast.LENGTH_LONG).show(); } }
关于public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)说明
Parameters
destinationAddress 发送短信的地址(也就是号码)
scAddress 短信服务中心,如果为null,就是用当前默认的短信服务中心
text 短信内容
sentIntent 如果不为null,当短信发送成功或者失败时,这个PendingIntent会被广播出去成功的结果代码是Activity.RESULT_OK,或者下面这些错误之一 :RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
对于 RESULT_ERROR_GENERIC_FAILURE, the这个sentIntent可能包括额外的”errorCode”,包含一些具体有用的信息帮助检查 。基于SMS控制的全部程序检查 sentIntent. 如果 sentIntent 为空,the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.
deliveryIntent 如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播,状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据(”pdu”)
相关文章推荐
- Android学习之AndroidManifest.xml清单之<uses-feature>
- 使用Volley 发送 ProtoBuf数据
- Android学习记录(八) alertDialog和popupwindow简单实现
- 十、命令设计模式
- Android 沉浸式状态栏 实现方式二 ( 更简单 )
- Android的Depth-LIB-Android的使用
- Android录制声音文件(音频),并播放
- android Criteria
- Android一句话 | ViewGroup事件分发
- Android一句话 | View事件分发
- 对 Android 开发者有益的 40 条优化建议
- Android手机震动
- 学习笔记(九)内容提供器
- Android提升篇系列:adb无法识别MX5等特殊机型
- Android例子源码非第三方实现根据字母排序的城市列表
- Android监听蓝牙耳机的按键事件
- Android Studio无法关联Api23源码-提示Souces for android api 23 platform not found
- android.view.WindowManager$BadTokenException: Unable to add window — token null
- Android系统构架分析和应用程序目录结构分析
- AndroidStudio简单快速导入GitHub中的第三方插件