• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android 读取、接收、发送 手机短信

武飞扬头像
「已注销」
帮助1

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 主要有:

  1.  
    content://sms/          所有短信
  2.  
    content://sms/inbox     收件箱
  3.  
    content://sms/sent      已发送
  4.  
    content://sms/draft     草稿
  5.  
    content://sms/outbox    发件箱
  6.  
    content://sms/failed    发送失败
  7.  
    content://sms/queued    待发送列表

SMS 数据库中的字段如下:

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

示例代码:

  1.  
    package com.lmy.sms;
  2.  
     
  3.  
    import java.sql.Date;
  4.  
    import java.text.SimpleDateFormat;
  5.  
     
  6.  
    import android.app.Activity;
  7.  
    import android.database.Cursor;
  8.  
    import android.database.sqlite.SQLiteException;
  9.  
    import android.net.Uri;
  10.  
    import android.os.Bundle;
  11.  
    import android.util.Log;
  12.  
    import android.widget.ScrollView;
  13.  
    import android.widget.TextView;
  14.  
     
  15.  
    public class SmsReadActivity extends Activity {
  16.  
     
  17.  
    @Override
  18.  
    public void onCreate(Bundle savedInstanceState) {
  19.  
    super.onCreate(savedInstanceState);
  20.  
     
  21.  
    TextView tv = new TextView(this);
  22.  
    tv.setText(getSmsInPhone());
  23.  
     
  24.  
    ScrollView sv = new ScrollView(this);
  25.  
    sv.addView(tv);
  26.  
     
  27.  
    setContentView(sv);
  28.  
    }
  29.  
     
  30.  
    public String getSmsInPhone() {
  31.  
    final String SMS_URI_ALL = "content://sms/"; // 所有短信
  32.  
    final String SMS_URI_INBOX = "content://sms/inbox"; // 收件箱
  33.  
    final String SMS_URI_SEND = "content://sms/sent"; // 已发送
  34.  
    final String SMS_URI_DRAFT = "content://sms/draft"; // 草稿
  35.  
    final String SMS_URI_OUTBOX = "content://sms/outbox"; // 发件箱
  36.  
    final String SMS_URI_FAILED = "content://sms/failed"; // 发送失败
  37.  
    final String SMS_URI_QUEUED = "content://sms/queued"; // 待发送列表
  38.  
     
  39.  
    StringBuilder smsBuilder = new StringBuilder();
  40.  
     
  41.  
    try {
  42.  
    Uri uri = Uri.parse(SMS_URI_ALL);
  43.  
    String[] projection = new String[] { "_id", "address", "person",
  44.  
    "body", "date", "type", };
  45.  
    Cursor cur = getContentResolver().query(uri, projection, null,
  46.  
    null, "date desc"); // 获取手机内部短信
  47.  
    // 获取短信中最新的未读短信
  48.  
    // Cursor cur = getContentResolver().query(uri, projection,
  49.  
    // "read = ?", new String[]{"0"}, "date desc");
  50.  
    if (cur.moveToFirst()) {
  51.  
    int index_Address = cur.getColumnIndex("address");
  52.  
    int index_Person = cur.getColumnIndex("person");
  53.  
    int index_Body = cur.getColumnIndex("body");
  54.  
    int index_Date = cur.getColumnIndex("date");
  55.  
    int index_Type = cur.getColumnIndex("type");
  56.  
     
  57.  
    do {
  58.  
    String strAddress = cur.getString(index_Address);
  59.  
    int intPerson = cur.getInt(index_Person);
  60.  
    String strbody = cur.getString(index_Body);
  61.  
    long longDate = cur.getLong(index_Date);
  62.  
    int intType = cur.getInt(index_Type);
  63.  
     
  64.  
    SimpleDateFormat dateFormat = new SimpleDateFormat(
  65.  
    "yyyy-MM-dd hh:mm:ss");
  66.  
    Date d = new Date(longDate);
  67.  
    String strDate = dateFormat.format(d);
  68.  
     
  69.  
    String strType = "";
  70.  
    if (intType == 1) {
  71.  
    strType = "接收";
  72.  
    } else if (intType == 2) {
  73.  
    strType = "发送";
  74.  
    } else if (intType == 3) {
  75.  
    strType = "草稿";
  76.  
    } else if (intType == 4) {
  77.  
    strType = "发件箱";
  78.  
    } else if (intType == 5) {
  79.  
    strType = "发送失败";
  80.  
    } else if (intType == 6) {
  81.  
    strType = "待发送列表";
  82.  
    } else if (intType == 0) {
  83.  
    strType = "所以短信";
  84.  
    } else {
  85.  
    strType = "null";
  86.  
    }
  87.  
     
  88.  
    smsBuilder.append("[ ");
  89.  
    smsBuilder.append(strAddress ", ");
  90.  
    smsBuilder.append(intPerson ", ");
  91.  
    smsBuilder.append(strbody ", ");
  92.  
    smsBuilder.append(strDate ", ");
  93.  
    smsBuilder.append(strType);
  94.  
    smsBuilder.append(" ]\n\n");
  95.  
    } while (cur.moveToNext());
  96.  
     
  97.  
    if (!cur.isClosed()) {
  98.  
    cur.close();
  99.  
    cur = null;
  100.  
    }
  101.  
    } else {
  102.  
    smsBuilder.append("no result!");
  103.  
    }
  104.  
     
  105.  
    smsBuilder.append("getSmsInPhone has executed!");
  106.  
     
  107.  
    } catch (SQLiteException ex) {
  108.  
    Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
  109.  
    }
  110.  
     
  111.  
    return smsBuilder.toString();
  112.  
    }
  113.  
    }
学新通

2、Android 接收短信

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

  1.  
    package com.lmy.SmsListener;
  2.  
     
  3.  
    import android.app.Activity;
  4.  
    import android.content.Intent;
  5.  
    import android.os.Bundle;
  6.  
    import android.widget.TextView;
  7.  
     
  8.  
    public class SmsListenerActivity extends Activity {
  9.  
     
  10.  
    @Override
  11.  
    public void onCreate(Bundle savedInstanceState) {
  12.  
    super.onCreate(savedInstanceState);
  13.  
    // setContentView(R.layout.main);
  14.  
    TextView tv = new TextView(this);
  15.  
    tv.setText("Hello. I started!");
  16.  
    setContentView(tv);
  17.  
    Intent service = new Intent(this, MyService.class);
  18.  
    this.startService(service);
  19.  
    }
  20.  
    }
学新通

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

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

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

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

  1.  
    package com.lmy.SmsListener;
  2.  
     
  3.  
    import android.content.BroadcastReceiver;
  4.  
    import android.content.Context;
  5.  
    import android.content.Intent;
  6.  
    import android.util.Log;
  7.  
     
  8.  
    public class MyBrocast extends BroadcastReceiver {
  9.  
     
  10.  
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  11.  
     
  12.  
    @Override
  13.  
    public void onReceive(Context context, Intent intent) {
  14.  
    Log.v("dimos", "MyBrocast");
  15.  
    if (intent.getAction().equals(ACTION)) {
  16.  
    Intent service = new Intent(context, MyService.class);
  17.  
    context.startService(service);
  18.  
    }
  19.  
    }
  20.  
     
  21.  
    }
学新通

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

  1.  
    package com.lmy.SmsListener;
  2.  
     
  3.  
    import android.app.Service;
  4.  
    import android.content.Intent;
  5.  
    import android.content.IntentFilter;
  6.  
    import android.os.IBinder;
  7.  
    import android.util.Log;
  8.  
     
  9.  
    public class MyService extends Service {
  10.  
     
  11.  
    @Override
  12.  
    public IBinder onBind(Intent intent) {
  13.  
    return null;
  14.  
    }
  15.  
     
  16.  
    @Override
  17.  
    public void onCreate() {
  18.  
    super.onCreate();
  19.  
    IntentFilter localIntentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
  20.  
    localIntentFilter.setPriority(2147483647);
  21.  
    SmsRecevier localMessageReceiver = new SmsRecevier();
  22.  
    Log.v("dimos", "MyService");
  23.  
    registerReceiver(localMessageReceiver, localIntentFilter);
  24.  
    }
  25.  
     
  26.  
     
  27.  
    }
学新通

广播接收到短信:

  1.  
    package com.lmy.SmsListener;
  2.  
     
  3.  
    import android.content.BroadcastReceiver;
  4.  
    import android.content.Context;
  5.  
    import android.content.Intent;
  6.  
    import android.util.Log;
  7.  
     
  8.  
    public class SmsRecevier extends BroadcastReceiver {
  9.  
     
  10.  
    public SmsRecevier() {
  11.  
    super();
  12.  
    Log.v("dimos", "SmsRecevier create");
  13.  
    }
  14.  
     
  15.  
    @Override
  16.  
    public void onReceive(Context context, Intent intent) {
  17.  
     
  18.  
    String dString = SmsHelper.getSmsBody(intent);
  19.  
    String address = SmsHelper.getSmsAddress(intent);
  20.  
    Log.i("dimos", dString "," address);
  21.  
    //阻止广播继续传递,如果该receiver比系统的级别高,
  22.  
    //那么系统就不会收到短信通知了
  23.  
     
  24.  
    abortBroadcast();
  25.  
    }
  26.  
    }
学新通

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

  1.  
    package com.lmy.SmsListener;
  2.  
     
  3.  
    import android.content.Intent;
  4.  
    import android.os.Bundle;
  5.  
    import android.telephony.SmsMessage;
  6.  
     
  7.  
    public class SmsHelper {
  8.  
    /**
  9.  
    * 获得短信内容
  10.  
    * */
  11.  
    public static String getSmsBody(Intent intent) {
  12.  
     
  13.  
    String tempString = "";
  14.  
    Bundle bundle = intent.getExtras();
  15.  
    Object messages[] = (Object[]) bundle.get("pdus");
  16.  
    SmsMessage[] smsMessage = new SmsMessage[messages.length];
  17.  
    for (int n = 0; n < messages.length; n ) {
  18.  
    smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
  19.  
    // 短信有可能因为使用了回车而导致分为多条,所以要加起来接受
  20.  
    tempString = smsMessage[n].getDisplayMessageBody();
  21.  
    }
  22.  
    return tempString;
  23.  
     
  24.  
    }
  25.  
     
  26.  
    /**
  27.  
    * 获得短信地址
  28.  
    * */
  29.  
    public static String getSmsAddress(Intent intent) {
  30.  
     
  31.  
    Bundle bundle = intent.getExtras();
  32.  
    Object messages[] = (Object[]) bundle.get("pdus");
  33.  
    return SmsMessage.createFromPdu((byte[]) messages[0])
  34.  
    .getDisplayOriginatingAddress();
  35.  
    }
  36.  
    }
学新通

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

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <manifest
  3.  
    xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
    package="com.lmy.SmsListener"
  5.  
    android:versionCode="1"
  6.  
    android:versionName="1.0">
  7.  
    <uses-sdk
  8.  
    android:minSdkVersion="7" />
  9.  
     
  10.  
    <application
  11.  
    android:icon="@drawable/icon"
  12.  
    android:label="@string/app_name">
  13.  
    <activity
  14.  
    android:name=".SmsListenerActivity"
  15.  
    android:label="@string/app_name">
  16.  
    <intent-filter>
  17.  
    <action
  18.  
    android:name="android.intent.action.MAIN" />
  19.  
    <category
  20.  
    android:name="android.intent.category.LAUNCHER" />
  21.  
    </intent-filter>
  22.  
    </activity>
  23.  
     
  24.  
    <receiver
  25.  
    android:name="MyBrocast"
  26.  
    android:enabled="true">
  27.  
    <intent-filter>
  28.  
    <action
  29.  
    android:name="android.intent.action.BOOT_COMPLETED" />
  30.  
    </intent-filter>
  31.  
     
  32.  
    </receiver>
  33.  
    <service android:name="MyService"></service>
  34.  
     
  35.  
    </application>
  36.  
    <uses-permission
  37.  
    android:name="android.permission.RECEIVE_SMS" /><!-- 接收短信权限 -->
  38.  
    <!-- 添加接收系统启动消息(用于开机启动)权限 -->
  39.  
    <uses-permission
  40.  
    android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  41.  
    </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 查移动话费余额,贴上代码:

  1.  
    package com.dimos.sendmessage;
  2.  
     
  3.  
    import java.util.ArrayList;
  4.  
     
  5.  
    import android.app.Activity;
  6.  
    import android.app.PendingIntent;
  7.  
    import android.content.Intent;
  8.  
    import android.content.IntentFilter;
  9.  
    import android.os.Bundle;
  10.  
    import android.telephony.SmsManager;
  11.  
     
  12.  
    public class SendMessageActivity extends Activity {
  13.  
     
  14.  
    @Override
  15.  
    public void onCreate(Bundle savedInstanceState) {
  16.  
    super.onCreate(savedInstanceState);
  17.  
    setContentView(R.layout.main);
  18.  
     
  19.  
    SendReceiver receiver=new SendReceiver();
  20.  
    IntentFilter filter=new IntentFilter();
  21.  
    filter.addAction(SendReceiver.ACTION);
  22.  
    registerReceiver(receiver,filter);
  23.  
    //必须先注册广播接收器,否则接收不到发送结果
  24.  
     
  25.  
    SmsManager smsManager = SmsManager.getDefault();
  26.  
    Intent intent = new Intent();
  27.  
    intent.setAction(SendReceiver.ACTION);
  28.  
    ArrayList<String> divideMessage = smsManager.divideMessage("ye");
  29.  
    PendingIntent sentIntent = PendingIntent.getBroadcast(this, 1, intent,
  30.  
    PendingIntent.FLAG_UPDATE_CURRENT);
  31.  
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
  32.  
    sentIntents.add(sentIntent);
  33.  
     
  34.  
    try {
  35.  
    smsManager.sendMultipartTextMessage("10086", null,
  36.  
    divideMessage, sentIntents, null);
  37.  
    } catch (Exception e) {
  38.  
    e.printStackTrace();
  39.  
    }
  40.  
    }
  41.  
    }
学新通

接收器:

  1.  
    package com.dimos.sendmessage;
  2.  
     
  3.  
    import android.app.Activity;
  4.  
    import android.content.BroadcastReceiver;
  5.  
    import android.content.Context;
  6.  
    import android.content.Intent;
  7.  
     
  8.  
    public class SendReceiver extends BroadcastReceiver {
  9.  
     
  10.  
    public static final String ACTION = "action.send.sms";
  11.  
     
  12.  
    @Override
  13.  
    public void onReceive(Context context, Intent intent) {
  14.  
    String action = intent.getAction();
  15.  
    if (ACTION.equals(action)) {
  16.  
    int resultCode = getResultCode();
  17.  
    if (resultCode == Activity.RESULT_OK) {
  18.  
    // 发送成功
  19.  
    System.out.println("发送成功!");
  20.  
    } else {
  21.  
    // 发送失败
  22.  
    System.out.println("发送失败!");
  23.  
    }
  24.  
    }
  25.  
    }
  26.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgakfff
系列文章
更多 icon
同类精品
更多 icon
继续加载