android獲取短信驗證碼並自動填寫的實現一

android獲取短信驗證碼並自動填寫的實現一

 
 
 

android獲取短信驗證碼並自動填寫的實現一,有須要的朋友能夠參考下。php

 

如今的應用在註冊登陸或者修改密碼中都用到了短信驗證碼,那在android中是如何實現獲取短信驗證碼並自動填寫的呢?java

首先,須要要在manifest中註冊接收和讀取短信的權限:android

 

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


實現一個廣播SMSBroadcastReceiver來監聽短信:app

 

package com.example.receive;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;


/**
 * 短信監聽
 * @author 
 *
 */
public class SMSBroadcastReceiver extends BroadcastReceiver {
	
    private static MessageListener mMessageListener;
    public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
	
	public SMSBroadcastReceiver() {
        super();
    }

	@Override
	public void onReceive(Context context, Intent intent) {
	        if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
	            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
	            for(Object pdu:pdus) {
	                SmsMessage smsMessage = SmsMessage.createFromPdu((byte [])pdu);
	                String sender = smsMessage.getDisplayOriginatingAddress();
	                //短信內容
	                String content = smsMessage.getDisplayMessageBody();
	                long date = smsMessage.getTimestampMillis(); Date tiemDate = new Date(date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = simpleDateFormat.format(tiemDate); //過濾不須要讀取的短信的發送號碼 if ("+8613450214963".equals(sender)) { mMessageListener.onReceived(content); abortBroadcast(); } } } } //回調接口 public interface MessageListener { public void onReceived(String message); } public void setOnReceivedMessageListener(MessageListener messageListener) { this.mMessageListener = messageListener; } } 


在須要填寫驗證碼的Activity中,生產SMSBroadcastReceiver的實例,實現onReceived的回調接口。爲了節約系統資源,咱們使用動態註冊註銷廣播的方法。ide

package com.example.smstest;

import com.example.receive.SMSBroadcastReceiver;

import android.os.Bundle;
import android.app.Activity;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private EditText edtPassword;
	private SMSBroadcastReceiver mSMSBroadcastReceiver;
	
	private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		edtPassword = (EditText) findViewById(R.id.password);
	}
	
	@Override
	protected void onStart() {
        super.onStart();
        //生成廣播處理
        mSMSBroadcastReceiver = new SMSBroadcastReceiver();

        //實例化過濾器並設置要過濾的廣播
        IntentFilter intentFilter = new IntentFilter(ACTION);
        intentFilter.setPriority(Integer.MAX_VALUE);
        //註冊廣播
        this.registerReceiver(mSMSBroadcastReceiver, intentFilter);

        mSMSBroadcastReceiver.setOnReceivedMessageListener(new SMSBroadcastReceiver.MessageListener() {
            @Override
            public void onReceived(String message) {

            	edtPassword.setText(message);

            }
        });
    }
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		 //註銷短信監聽廣播
        this.unregisterReceiver(mSMSBroadcastReceiver);
	}


}
相關文章
相關標籤/搜索