wemall app商城原始碼Android簡訊監聽接收器

weixin_34127717發表於2016-11-26

wemall doraemon是Android客戶端程式,服務端採用wemall微信商城,不對原商城做任何修改,只需要在原商城目錄下上傳介面檔案即可完成服務端的配置,客戶端可隨阿意定製修改。本文分享其中簡訊監聽接收器,用於自動獲取簡訊驗證碼,然後自動填寫到驗證碼區域程式碼,供技術員參考學習。

package cn.smssdk.gui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import cn.smssdk.SMSSDK;
/** 簡訊監聽接收器,用於自動獲取簡訊驗證碼,然後自動填寫到驗證碼區域*/
public class SMSReceiver extends BroadcastReceiver {

    private static final String ACTION_SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";

    private SMSSDK.VerifyCodeReadListener listener;
    public SMSReceiver(SMSSDK.VerifyCodeReadListener verifyCodeReadListener) {
        this.listener = verifyCodeReadListener;
    }


    /**
     * 不要使用AndroidManifest.xml配置的方式註冊Receiver,
     * 請使用Context.registerReceiver註冊監聽器, 因為初始化的時候要傳入監聽器
     */
    public SMSReceiver() {
        String msg = "Please dynamically register an instance of this class with Context.registerReceiver."
                +"\r\nIf not, the SMSSDK.VerifyCodeReadListener will be null!";
        Log.w("cn.smssdk.gui.SMSReceiver", msg);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(ACTION_SMS_RECEIVER.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            if(bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] smsArr = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    smsArr[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                for (SmsMessage sms: smsArr) {
                    if(sms != null) {
                        SMSSDK.readVerificationCode(sms, listener);
                    }
                }
            }// END if(bundle != null)
        }
    }
}

原文詳情地址:http://Git.oschina.NET/zzunet...
wemall doraemonAndroid app商城詳情地址:http://www.koahub.com/home/pr...
wemall官網地址:http://www.wemallshop.com

相關文章