Android短信验证码自动填入解决方案之一:BroadcastReceiver

来源:互联网 发布:怎么在淘宝买种子 编辑:程序博客网 时间:2024/06/05 05:00
package com.example.asia.testbroadcastreceiver;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.content.LocalBroadcastManager;import android.telephony.SmsMessage;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private static final int UNREGISTER_RECEIVER = 0X777;    private SmsBroadcastReceiver smsBroadcastReceiver;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /**     *  注册账户,返回验证码     * @param view     */    public void registerNewAccount(View view){        //发送给服务器,进行注册(代码略、、、、)        //动态注册手机短信广播接收者        smsBroadcastReceiver = new SmsBroadcastReceiver();        IntentFilter filter = new IntentFilter();        filter.addAction("android.provider.Telephony.SMS_RECEIVED");        registerReceiver(smsBroadcastReceiver, filter);        /**         *60s后如果还没有收到短信,就反注册广播接收者,这是为了         * 安全起见,如果服务器抽风没有给手机发短信,就使其失效。         */        handler.sendEmptyMessageDelayed(UNREGISTER_RECEIVER, 60 * 1000);    }    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            switch (msg.what){                case UNREGISTER_RECEIVER:                    if(smsBroadcastReceiver != null){                        unregisterReceiver(smsBroadcastReceiver);                        Toast.makeText(MainActivity.this, "服务器抽风了,你可以选择重新注册",Toast.LENGTH_SHORT).show();                    }                    break;            }        }    };    public class SmsBroadcastReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            //短信中的内容封装在bundle中,谷歌怎么存的,我们就这么取出来            Bundle bundle = intent.getExtras();            /**             * 取出bundle中数据,根据key-value方式取出来。             * key是pdus,pdu是协议数据单元,映射到Android中就是一条短信中的内容包含了短信相关的             * 诸多信息,如短信内容、发送者的号码等。多条短信就             * 使用pdus代替,返回的是一个数组。             */            Object[] objects = (Object[]) bundle.get("pdus");            for(Object object : objects){                //使用这个方法取出短信内容及手机号码                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) object);                String phone = smsMessage.getOriginatingAddress();                String sms = smsMessage.getMessageBody();                //假设我们服务端发短信的号码为999511                if("999511".equals(phone) && sms.startsWith("你输入的验证码为")){                   ((EditText)findViewById(R.id.vaild_code)).setText(sms.substring(sms.length()-4));                    if(smsBroadcastReceiver != null){                        unregisterReceiver(smsBroadcastReceiver);                    }                }            }        }    }}

xml布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     tools:context="com.example.asia.testbroadcastreceiver.MainActivity">    <EditText        android:hint="请输入验证码"        android:id="@+id/vaild_code"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <Button        android:onClick="registerNewAccount"        android:text="registerNewAccount"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

结果:

这里写图片描述

0 0
原创粉丝点击