首页 > 代码库 > Android之发送短信和接收验证码

Android之发送短信和接收验证码

  最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录

 

   前提---权限

   

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

   

  1.   发送短信并提示是否发送成功以及对方是否接收

    package com.javen.utils;import java.util.ArrayList;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.telephony.SmsManager;import android.widget.Toast;/** * @author Javen * */public class SendMessageUtil {    /** 发送与接收的广播 **/    private static String SENT_SMS_ACTION = "SENT_SMS_ACTION";    private static String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";    /**     * 实现发送短信     * @param context      * @param text 短信的内容     * @param phoneNumber 手机号码     */    public static void sendMessage(Context context, String text,            String phoneNumber) {        context.registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));        context.registerReceiver(receiver, new IntentFilter(                DELIVERED_SMS_ACTION));                // create the sentIntent parameter          Intent sentIntent = new Intent(SENT_SMS_ACTION);          PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,0);        // create the deilverIntent parameter          Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);          PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,deliverIntent, 0);                 SmsManager smsManager = SmsManager.getDefault();        //如果字数超过5,需拆分成多条短信发送        if (text.length() > 70 ) {            ArrayList<String> msgs = smsManager.divideMessage(text);            for (String msg : msgs) {                smsManager.sendTextMessage(phoneNumber, null, msg, sentPI, deliverPI);                                    }        } else {            smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);        }    }    private static BroadcastReceiver sendMessage = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            // 判断短信是否发送成功            switch (getResultCode()) {            case Activity.RESULT_OK:                Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();                break;            default:                Toast.makeText(context, "发送失败", Toast.LENGTH_LONG).show();                break;            }        }    };    private static BroadcastReceiver receiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            // 表示对方成功收到短信            Toast.makeText(context, "对方接收成功", Toast.LENGTH_LONG).show();        }    };}

     


  2. 通过短信数据库获取短信内容

    package com.example.message;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.app.Activity;import android.content.ContentResolver;import android.content.Context;import android.database.ContentObserver;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.widget.Toast;public class MainActivity extends Activity {    private SmsObserver smsObserver;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        smsObserver = new SmsObserver(this, smsHandler);        getContentResolver().registerContentObserver(SMS_INBOX, true,smsObserver);    }    public Handler smsHandler = new Handler() {        // 这里可以进行回调的操作        // TODO        public void handleMessage(android.os.Message msg) {            System.out.println("smsHandler 执行了.....");        };    };    private Uri SMS_INBOX = Uri.parse("content://sms/");    public void getSmsFromPhone() {        ContentResolver cr = getContentResolver();        String[] projection = new String[] { "body","address","person"};// "_id", "address",                                                        // "person",, "date",                                                        // "type        String where = " date >  "                + (System.currentTimeMillis() - 10 * 60 * 1000);        Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc");        if (null == cur)            return;        if (cur.moveToNext()) {            String number = cur.getString(cur.getColumnIndex("address"));// 手机号            String name = cur.getString(cur.getColumnIndex("person"));// 联系人姓名列表            String body = cur.getString(cur.getColumnIndex("body"));                        System.out.println(">>>>>>>>>>>>>>>>手机号:" + number);            System.out.println(">>>>>>>>>>>>>>>>联系人姓名列表:" + name);            System.out.println(">>>>>>>>>>>>>>>>短信的内容:" + body);                        // 这里我是要获取自己短信服务号码中的验证码~~            Pattern pattern = Pattern.compile("[a-zA-Z0-9]{5}");            Matcher matcher = pattern.matcher(body);//String body="测试验证码2346ds";            if (matcher.find()) {                String res = matcher.group().substring(0, 5);// 获取短信的内容                showToast(res);                System.out.println(res);            }        }    }    protected void showToast(String text) {        Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();    }    class SmsObserver extends ContentObserver {        public SmsObserver(Context context, Handler handler) {            super(handler);        }        @Override        public void onChange(boolean selfChange) {            super.onChange(selfChange);            // 每当有新短信到来时,使用我们获取短消息的方法            getSmsFromPhone();        }    }}

     

    sms主要结构:  
      
      _id:短信序号,如100
      
      thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
      
      address:发件人地址,即手机号,如+86138138000
      
      person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
      
      date:日期,long型,如1346988516,可以对日期显示格式进行设置
      
      protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信
      
      read:是否阅读0未读,1已读
      
      status:短信状态-1接收,0complete,64pending,128failed
      
      type:短信类型1是接收到的,2是已发出
      
      body:短信具体内容
      
      service_center:短信服务中心号码编号,如+8613800755500

     
    详细的数据库文件如下:

 

      

短信数据库分析

 

表结构分析:

address:短信发送者电话号码

person:联系人编号,如果电话薄里有联系人则显示编号,没有联系人则显示null

read:读取状态,0为未读,1为已读

body:短信内容

 

thread表:线程表

 

dataL:日期

message_count:短信发送的条数

snippet:最后一条短信内容

read:短信读取状态