Android短信监听接收器插入

OSC_RjDivW 发布于 2016/11/26 16:24
阅读 178
收藏 1

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-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

加载中
返回顶部
顶部