:https://www.cnblogs.com/ycclmy/tag/android/

1、Android 读取手机短信

From:https://www.cnblogs.com/ycclmy/p/3193075.html

获取 android 手机短信需要在 AndroidManifest.xml 加权限:

<uses-permission android:name="android.permission.READ_SMS" />

获取短信只需要得到 ContentResolver 就行了,它的 URI 主要有:

content://sms/          所有短信
content://sms/inbox     收件箱
content://sms/sent      已发送
content://sms/draft     草稿
content://sms/outbox    发件箱
content://sms/failed    发送失败
content://sms/queued    待发送列表

SMS 数据库中的字段如下:

_id        一个自增字段,从1开始
thread_id  序号,同一发信人的id相同
address    发件人手机号码
person     联系人列表里的序号,陌生人为null
date       发件日期
protocol   协议,分为: 0 SMS_RPOTO, 1 MMS_PROTO
read       是否阅读 0未读, 1已读
status     状态 -1接收,0 complete, 64 pending, 128 failed
type       ALL = 0;INBOX = 1;SENT = 2;DRAFT = 3;OUTBOX = 4;FAILED = 5; QUEUED = 6;
body       短信内容
service_center 短信服务中心号码编号。如+8613800755500
subject        短信的主题
reply_path_present TP-Reply-Path
locked

示例代码:

package com.lmy.sms;import java.sql.Date;
import java.text.SimpleDateFormat;import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;public class SmsReadActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView tv = new TextView(this);tv.setText(getSmsInPhone());ScrollView sv = new ScrollView(this);sv.addView(tv);setContentView(sv);}public String getSmsInPhone() {final String SMS_URI_ALL = "content://sms/"; // 所有短信final String SMS_URI_INBOX = "content://sms/inbox"; // 收件箱final String SMS_URI_SEND = "content://sms/sent"; // 已发送final String SMS_URI_DRAFT = "content://sms/draft"; // 草稿final String SMS_URI_OUTBOX = "content://sms/outbox"; // 发件箱final String SMS_URI_FAILED = "content://sms/failed"; // 发送失败final String SMS_URI_QUEUED = "content://sms/queued"; // 待发送列表StringBuilder smsBuilder = new StringBuilder();try {Uri uri = Uri.parse(SMS_URI_ALL);String[] projection = new String[] { "_id", "address", "person","body", "date", "type", };Cursor cur = getContentResolver().query(uri, projection, null,null, "date desc"); // 获取手机内部短信// 获取短信中最新的未读短信// Cursor cur = getContentResolver().query(uri, projection,// "read = ?", new String[]{"0"}, "date desc");if (cur.moveToFirst()) {int index_Address = cur.getColumnIndex("address");int index_Person = cur.getColumnIndex("person");int index_Body = cur.getColumnIndex("body");int index_Date = cur.getColumnIndex("date");int index_Type = cur.getColumnIndex("type");do {String strAddress = cur.getString(index_Address);int intPerson = cur.getInt(index_Person);String strbody = cur.getString(index_Body);long longDate = cur.getLong(index_Date);int intType = cur.getInt(index_Type);SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");Date d = new Date(longDate);String strDate = dateFormat.format(d);String strType = "";if (intType == 1) {strType = "接收";} else if (intType == 2) {strType = "发送";} else if (intType == 3) {strType = "草稿";} else if (intType == 4) {strType = "发件箱";} else if (intType == 5) {strType = "发送失败";} else if (intType == 6) {strType = "待发送列表";} else if (intType == 0) {strType = "所以短信";} else {strType = "null";}smsBuilder.append("[ ");smsBuilder.append(strAddress + ", ");smsBuilder.append(intPerson + ", ");smsBuilder.append(strbody + ", ");smsBuilder.append(strDate + ", ");smsBuilder.append(strType);smsBuilder.append(" ]\n\n");} while (cur.moveToNext());if (!cur.isClosed()) {cur.close();cur = null;}} else {smsBuilder.append("no result!");}smsBuilder.append("getSmsInPhone has executed!");} catch (SQLiteException ex) {Log.d("SQLiteException in getSmsInPhone", ex.getMessage());}return smsBuilder.toString();}
}

2、Android 接收短信

启动程序时启动一个 service,在 service 里注册接收短信的广播,当手机收到短信里,打印出短信内容跟电话号码。

package com.lmy.SmsListener;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;public class SmsListenerActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);TextView tv = new TextView(this);tv.setText("Hello. I started!");setContentView(tv);Intent service = new Intent(this, MyService.class);this.startService(service);}
}

当 service 被 kill 后,我们可以在开机时自动启动 service。

开机自动启动一个 service,在 service 里注册接收短信的广播,当手机收到短信里,打印出短信内容跟电话号码。

开机启动后系统会发出一个 Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个 Action 只会发出一次。

创建一个类继承 BroadcastReceiver,在 onReceive(Context context, Intent intent) 里面启动service。

package com.lmy.SmsListener;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;public class MyBrocast extends BroadcastReceiver {static final String ACTION = "android.intent.action.BOOT_COMPLETED";@Overridepublic void onReceive(Context context, Intent intent) {Log.v("dimos", "MyBrocast");if (intent.getAction().equals(ACTION)) {Intent service = new Intent(context, MyService.class);context.startService(service);}}}

在 service 中注册一个接收短信的广播:

package com.lmy.SmsListener;import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();IntentFilter localIntentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");localIntentFilter.setPriority(2147483647);SmsRecevier localMessageReceiver = new SmsRecevier();Log.v("dimos", "MyService");registerReceiver(localMessageReceiver, localIntentFilter);}}

广播接收到短信:

package com.lmy.SmsListener;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;public class SmsRecevier extends BroadcastReceiver {public SmsRecevier() {super();Log.v("dimos", "SmsRecevier create");}@Overridepublic void onReceive(Context context, Intent intent) {String dString = SmsHelper.getSmsBody(intent);String address = SmsHelper.getSmsAddress(intent);Log.i("dimos", dString+","+address);//阻止广播继续传递,如果该receiver比系统的级别高,//那么系统就不会收到短信通知了abortBroadcast(); }
}

获得短信内容跟短信地址:

package com.lmy.SmsListener;import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;public class SmsHelper {/*** 获得短信内容* */public static String getSmsBody(Intent intent) {String tempString = "";Bundle bundle = intent.getExtras();Object messages[] = (Object[]) bundle.get("pdus");SmsMessage[] smsMessage = new SmsMessage[messages.length];for (int n = 0; n < messages.length; n++) {smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);// 短信有可能因为使用了回车而导致分为多条,所以要加起来接受tempString += smsMessage[n].getDisplayMessageBody();}return tempString;}/*** 获得短信地址* */public static String getSmsAddress(Intent intent) {Bundle bundle = intent.getExtras();Object messages[] = (Object[]) bundle.get("pdus");return SmsMessage.createFromPdu((byte[]) messages[0]).getDisplayOriginatingAddress();}
}

在 AndroidManifest.xml 里声明并加权限:

<?xml version="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.lmy.SmsListener"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="7" /><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".SmsListenerActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><receiverandroid:name="MyBrocast"android:enabled="true"><intent-filter><actionandroid:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver><service android:name="MyService"></service></application><uses-permissionandroid:name="android.permission.RECEIVE_SMS" /><!-- 接收短信权限 --><!-- 添加接收系统启动消息(用于开机启动)权限 --><uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

这样就可以获得接收到的短信了。

3、Android SmsManager 发送短信

SmsManager 可以在后台发送短信,无需用户操作,开发者就用这个 SmsManager 功能在后台偷偷给SP发短信,导致用户话费被扣。必须添加 android.permission.SEND_SMS 权限。

<uses-permission android:name="android.permission.SEND_SMS" />

如果短信内容过长,可以使用 SmsManager.divideMessage(String text)方法自动拆分成一个ArrayList 数组,再根据数组长度循环发送。

用 sendMultipartTextMessage(String destinationAddress, string scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) 方法发送。参数分别为:号码,短信服务中心号码(null 即可),短信内容,短信发送结果广播PendingIntent,短信到达广播。

下面写一个 demo 查移动话费余额,贴上代码:

package com.dimos.sendmessage;import java.util.ArrayList;import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;public class SendMessageActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);SendReceiver receiver=new SendReceiver();IntentFilter filter=new IntentFilter();filter.addAction(SendReceiver.ACTION);registerReceiver(receiver,filter);//必须先注册广播接收器,否则接收不到发送结果SmsManager smsManager = SmsManager.getDefault();Intent intent = new Intent();intent.setAction(SendReceiver.ACTION);ArrayList<String> divideMessage = smsManager.divideMessage("ye");PendingIntent sentIntent = PendingIntent.getBroadcast(this, 1, intent,PendingIntent.FLAG_UPDATE_CURRENT);ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();sentIntents.add(sentIntent);try {smsManager.sendMultipartTextMessage("10086", null,divideMessage, sentIntents, null);} catch (Exception e) {e.printStackTrace();}}
}

接收器:

package com.dimos.sendmessage;import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;public class SendReceiver extends BroadcastReceiver {public static final String ACTION = "action.send.sms";@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (ACTION.equals(action)) {int resultCode = getResultCode();if (resultCode == Activity.RESULT_OK) {// 发送成功System.out.println("发送成功!");} else {// 发送失败System.out.println("发送失败!");}}}
}

Android 读取、接收、发送 手机短信相关推荐

  1. 通过短信猫发送手机短信

    wavecom短信猫常用AT命令 一.一般命令 1. AT+CGMI 给出模块厂商的标识. 2. AT+CGMM 获得模块标识.这个命令用来得到支持的频带 (GSM 900,DCS 1800 或PCS ...

  2. Java实现的一个发送手机短信(亲测可用)

    原文地址:http://sms.webchinese.cn/api.shtml JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webs ...

  3. Android 集成Mob实现手机短信验证码

    项目中集成MOB.com提供的三方短信SDK. 当然这些短信都是免费的.并且可以在您申请的MOB.com后台去查看短信注册用户的相关信息. 这篇文后半部分会提供一个简单的验证短信Demo,有界面,当然 ...

  4. java实现的一个发送手机短信

    利用java实现的一个发送手机短信的小例子 JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注 ...

  5. 通过PC发送手机短信

    手机短信发送²        中文转换成Unicode码函数 因为手机短消息的发送是以PDU串的形式发送出去的,中文字符以Unicode码来表示,所以在发送中文短消息之前必须首先将中文字符转换为Uni ...

  6. java发送手机短信demo

    JAVA发送手机短信有几种方法: (1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这 ...

  7. 为 Asp.net 网站新增发送手机短信功能

    本文旨在帮助那些为网站发送手机短信正在寻求解决方案还未最终找到解决方案的朋友提供参考. 适合人群 须满足一下条件之一,如果以下3个条件您都不满足,为节约您宝贵的时间,请终止阅读本篇文章. 条件如下: ...

  8. 利用java实现的一个发送手机短信的小例子

    今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看,写的不太相信,闲的没事,把他整理下来,以后可能用得着 JAVA发送手机短信,流传有几种方法:(1)使用webservi ...

  9. java实现语音发送,Java实现发送手机短信语音验证功能代码实例

    这篇文章主要介绍了java实现发送手机短信语音验证功能代码实例,www.cppcns.com文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 利用第三方 ...

  10. 短信网关 php,通过ICQ网关发送手机短信的PHP源程序_PHP

    (转自linuxforum.net 原作者:liushiliang EMAIL:  lsl@163.net ) //########################################## ...

最新文章

  1. Python搭建Keras CNN模型破解网站验证码
  2. Django项目配合sentry实现浅析
  3. keepalive实验配置
  4. android开发系列之数据存储
  5. SVN 图标和工具、wc.db学习
  6. 别为了学编程而学编程
  7. SAP 物料XXXXX的强制帐户设置 (输入帐户设置类别) 的问题解决方法
  8. 8086和8088微处理器之间的区别
  9. 不用加号实现两整数相加
  10. 建立RHELAS4下面的Yum本地源
  11. Linux CH340驱动安装
  12. Git服务器更换IP
  13. 关于--如何查看外网IP地址
  14. python 基因测序_使用机器学习和Python揭开DNA测序神秘面纱
  15. 使用Blynk打造一款物联网产品
  16. 【正点原子I.MX6U-MINI应用篇】6、嵌入式Linux在LCD屏幕上显示字符
  17. mac中手动切换go版本
  18. excel的主要功能_如何用excel制作表格?
  19. 【矩阵论】02——线性空间——基、维数与坐标
  20. 小白学习Linux命令

热门文章

  1. ACM 投稿时CCS CONCEPTS 生成及插入
  2. PCIE 设备在位检测机理
  3. 目标检测YOLO实战应用案例100讲-基于深度学习的交通场景多尺度目标检测算法研究与应用
  4. 计算机毕业设计springboot基于VUE电脑城摊位出租系统
  5. python tkinter编写界面,使用win32com操作excel获取数据生成截图后,wxpy登录微信,给租客发送房租(三)
  6. 【MM小贴士】SAP采购订单创建参考采购申请的强控
  7. 热加工作业考研题目答案分享——Joining processes 4
  8. nanotime java_Java System nanoTime()方法
  9. Codeforces Round #545 (Div. 2)
  10. 黑马程序员_工欲善其事必先利其器