标签:android des style blog http java
最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录
前提---权限
<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>
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(); } }; }
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主要结构:
详细的数据库文件如下:
表结构分析:
address:短信发送者电话号码
person:联系人编号,如果电话薄里有联系人则显示编号,没有联系人则显示null
read:读取状态,0为未读,1为已读
body:短信内容
thread表:线程表
dataL:日期
message_count:短信发送的条数
snippet:最后一条短信内容
read:短信读取状态
Android之发送短信和接收验证码,布布扣,bubuko.com
标签:android des style blog http java
原文地址:http://www.cnblogs.com/zyw-205520/p/3835369.html