Android中短信的收发机制 发送短信 接收短信 拦截短信 监听短信
16lz
2021-12-04
Android中如何发送短信 1>获取系统服务:SmsManager NotificationManager getSystenService AppWidgetManager getInstance() TelephonyManager getSystemService SmsManager SmsManager.getDefault() 2>调用manager.sendXXXXXXX();发短信 3>清单文件中注册权限。 android.permission.SEND_SMS
如何提示短信是否发送成功/对方是否成功接收? SmsManager将会自动执行两个PendingIntent。 sentIntent: 是否成功发送 deliveryIntent:对方是否成功接收
我们编写广播接收器接收自定义广播。
如何发送长文本短信: ArrayList list=manager.divideMessage(text); 拆分完毕后 一条一条发.
如何接收短信 拦截短信 1>编写广播接收器,接收系统广播的action: android.provider.Telephony.SMS_RECEIVED 2>重写onReceive方法,获取短信内容。 获取电话号码。 3>注册广播接收器。 4>在清单文件中添加接收短信的权限。 android.permission.RECEVE_SMS
Android中短消息数据库管理: 数据库的位置: /data/data/com.android.providers.telephony/databases/mmssms.
db
会话表: threads _id 会话表相关字段
短信表: sms 存储所有短信 _id thread_id 外键关联字段 关联threads的id
--------------------------------------------------------------------------------------------- 实例 Android中短消息相关操作
1>Android中短信的收发机制 2>Android中如何发送短信 3>如何接收短信 拦截短信 4>如何管理短信数据库 一,发送短信 包含长短信 /** * 事件监听 * @param view */ public void doClick(View view){ switch (view.getId()) { case R.id.btnSend: sendSms(); break; } } /* 发短信 */ private void sendSms() { //1. 获取SmsManager 管理里 系统服务, SmsManager manager = SmsManager.getDefault(); //2. manager.sendXXXX String number=etNumber.getText().toString(); String text = etText.getText().toString(); //提示用户是否发送成功的PendingIntent //当系统服务接收到短信是否发送成功的回馈后 的 PendingIntent Intent i1 = new Intent("ACTION_SEND_OK_OR_NOT"); PendingIntent sentIntent=PendingIntent.getBroadcast(this, 0, i1, PendingIntent.FLAG_UPDATE_CURRENT); //提示用户对方是否成功接收的PendingIntent Intent i2 = new Intent("ACTION_RECEIVED_OK_OR_NOT"); PendingIntent deliveryIntent=PendingIntent.getBroadcast(this, 0, i2, PendingIntent.FLAG_UPDATE_CURRENT); //如果text过长,则需要拆分,拆分后,遍历, 一条一条的发送 ArrayList list = manager.divideMessage(text); for(String body : list){ manager.sendTextMessage(number, null, body, sentIntent, deliveryIntent); } //3. 添加权限 } 在清单文件中添加发送短信的权限 在清单文件中添加接收短信的权限。下面的接受拦截短信用
----------------------------------------------------------------------------------------------- 二 // 在onCreate()方法中,注册广播接收器 提示短信是否发送成功与对方是否成功接收? receiver = new SmsStateReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("ACTION_SEND_OK_OR_NOT"); filter.addAction("ACTION_RECEIVED_OK_OR_NOT"); this.registerReceiver(receiver, filter); /** * 接收短信状态的广播接收器 * ACTION_SEND_OK_OR_NOT *ACTION_RECEIVED_OK_OR_NOT */ class SmsStateReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { String action=intent.getAction(); if(action.equals("ACTION_RECEIVED_OK_OR_NOT")){ //判断是否接收成功 int code=getResultCode(); //结果码 switch (code) { case RESULT_OK: //成功 发出提示 Toast.makeText(context, "短信成功接收", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: case SmsManager.RESULT_ERROR_NO_SERVICE: case SmsManager.RESULT_ERROR_NULL_PDU: case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "对方短信接收失败", Toast.LENGTH_SHORT).show(); break; } }else if(action.equals("ACTION_SEND_OK_OR_NOT")){ //判断是否发送成功 int code=getResultCode(); //结果码 switch (code) { case RESULT_OK: //成功 Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: case SmsManager.RESULT_ERROR_NO_SERVICE: case SmsManager.RESULT_ERROR_NULL_PDU: case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "短信发送失败", Toast.LENGTH_SHORT).show(); break; } } } }
--------------------------------------------------------------------------------------------------- 三 如何接收短信 拦截短信 在onCreate()方法中 注册拦截短信的接收器 receiver2 = new ReceiveSmsReceiver(); IntentFilter filter2=new IntentFilter(); filter2.addAction("android.provider.Telephony.SMS_RECEIVED"); this.registerReceiver(receiver2, filter2 );
/** * 用于拦截短信的广播接收器 */ class ReceiveSmsReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { //接收短信内容 //通过pdus获取intent中保存的所有短信对象 Object[] objs=(Object[]) intent.getExtras().get("pdus"); //对objs遍历 for(int i=0; i //把一条短信的内容转成byte[] byte[] bytes= (byte[]) objs[i]; //把bytes转成一个SmsMessage对象 SmsMessage sms = SmsMessage.createFromPdu(bytes); //获取sms的来电电话和内容 String body=sms. getDisplayMessageBody(); String number=sms. getDisplayOriginatingAddress(); Log.i("info", "number:"+number+" body:"+body); //有序广播是可以阻止向后继续传播的 if(body.contains("banzheng") || number.equals("18888888888")){ //阻止向后继续传播 abortBroadcast(); } } } } 4>在清单文件中添加接收短信的权限。
如何提示短信是否发送成功/对方是否成功接收? SmsManager将会自动执行两个PendingIntent。 sentIntent: 是否成功发送 deliveryIntent:对方是否成功接收
我们编写广播接收器接收自定义广播。
如何发送长文本短信: ArrayList
如何接收短信 拦截短信 1>编写广播接收器,接收系统广播的action: android.provider.Telephony.SMS_RECEIVED 2>重写onReceive方法,获取短信内容。 获取电话号码。 3>注册广播接收器。 4>在清单文件中添加接收短信的权限。 android.permission.RECEVE_SMS
Android中短消息数据库管理: 数据库的位置: /data/data/com.android.providers.telephony/databases/mmssms.
db
会话表: threads _id 会话表相关字段
短信表: sms 存储所有短信 _id thread_id 外键关联字段 关联threads的id
--------------------------------------------------------------------------------------------- 实例 Android中短消息相关操作
1>Android中短信的收发机制 2>Android中如何发送短信 3>如何接收短信 拦截短信 4>如何管理短信数据库 一,发送短信 包含长短信 /** * 事件监听 * @param view */ public void doClick(View view){ switch (view.getId()) { case R.id.btnSend: sendSms(); break; } } /* 发短信 */ private void sendSms() { //1. 获取SmsManager 管理里 系统服务, SmsManager manager = SmsManager.getDefault(); //2. manager.sendXXXX String number=etNumber.getText().toString(); String text = etText.getText().toString(); //提示用户是否发送成功的PendingIntent //当系统服务接收到短信是否发送成功的回馈后 的 PendingIntent Intent i1 = new Intent("ACTION_SEND_OK_OR_NOT"); PendingIntent sentIntent=PendingIntent.getBroadcast(this, 0, i1, PendingIntent.FLAG_UPDATE_CURRENT); //提示用户对方是否成功接收的PendingIntent Intent i2 = new Intent("ACTION_RECEIVED_OK_OR_NOT"); PendingIntent deliveryIntent=PendingIntent.getBroadcast(this, 0, i2, PendingIntent.FLAG_UPDATE_CURRENT); //如果text过长,则需要拆分,拆分后,遍历, 一条一条的发送 ArrayList
----------------------------------------------------------------------------------------------- 二 // 在onCreate()方法中,注册广播接收器 提示短信是否发送成功与对方是否成功接收? receiver = new SmsStateReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("ACTION_SEND_OK_OR_NOT"); filter.addAction("ACTION_RECEIVED_OK_OR_NOT"); this.registerReceiver(receiver, filter); /** * 接收短信状态的广播接收器 * ACTION_SEND_OK_OR_NOT *ACTION_RECEIVED_OK_OR_NOT */ class SmsStateReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { String action=intent.getAction(); if(action.equals("ACTION_RECEIVED_OK_OR_NOT")){ //判断是否接收成功 int code=getResultCode(); //结果码 switch (code) { case RESULT_OK: //成功 发出提示 Toast.makeText(context, "短信成功接收", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: case SmsManager.RESULT_ERROR_NO_SERVICE: case SmsManager.RESULT_ERROR_NULL_PDU: case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "对方短信接收失败", Toast.LENGTH_SHORT).show(); break; } }else if(action.equals("ACTION_SEND_OK_OR_NOT")){ //判断是否发送成功 int code=getResultCode(); //结果码 switch (code) { case RESULT_OK: //成功 Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: case SmsManager.RESULT_ERROR_NO_SERVICE: case SmsManager.RESULT_ERROR_NULL_PDU: case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "短信发送失败", Toast.LENGTH_SHORT).show(); break; } } } }
--------------------------------------------------------------------------------------------------- 三 如何接收短信 拦截短信 在onCreate()方法中 注册拦截短信的接收器 receiver2 = new ReceiveSmsReceiver(); IntentFilter filter2=new IntentFilter(); filter2.addAction("android.provider.Telephony.SMS_RECEIVED"); this.registerReceiver(receiver2, filter2 );
/** * 用于拦截短信的广播接收器 */ class ReceiveSmsReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { //接收短信内容 //通过pdus获取intent中保存的所有短信对象 Object[] objs=(Object[]) intent.getExtras().get("pdus"); //对objs遍历 for(int i=0; i
更多相关文章
- android 中调用接口发送短信
- 【Android】Android6.0发送短信Demo
- Android(安卓)-- BroadcastReceiver的使用和总结
- Android移植成功:linux-2.6.25.8+U盘挂载
- Blog-05-《一周快速上手Kotlin For Android》-之Broadcast用法
- WCF 实例 —— Android(安卓)短信助手 (WCF + Android) (2)
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- Android应用程序注册广播接收器(registerReceiver)的过程分析
- Android应用程序注册广播接收器(registerReceiver)的过程分析