java Android - BroadcastReceiver读取短信并发送带有代码读取的de.greenrobot.event.EventBus事件(消息应该只有代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android - BroadcastReceiver读取短信并发送带有代码读取的de.greenrobot.event.EventBus事件(消息应该只有代码相关的知识,希望对你有一定的参考价值。

public class ValidationCodeEvent {

    private String code;

    public ValidationCodeEvent(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

}
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import com.pickupnow.customer.broadcast_receivers.SmsCodeBroadcastReceiver;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class SmsCodeValidatorTests {

    @Test
    public void parseSmsCodes() {
        String code = SmsCodeBroadcastReceiver.manageSmsBody("This is a code 123123 and it should work");
        assertThat(code).isEqualTo("123123");
    }

    @Test
    public void noCodeReturnsNull() {
        String code = SmsCodeBroadcastReceiver.manageSmsBody("There is no code so should return null");
        assertThat(code).isNull();
    }

    @Test
    public void emptyBodyReturnsNull() {
        String code = SmsCodeBroadcastReceiver.manageSmsBody(null);
        assertThat(code).isNull();
    }

}
public class SmsCodeBroadcastReceiver extends BroadcastReceiver {

    public static IntentFilter getFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        return filter;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
            SmsMessage[] messages = getMessagesFromIntent(intent);
            for (SmsMessage message : messages) {
                String msgBody = message.getMessageBody();
                manageSmsBody(msgBody);
            }
        }
    }

    @VisibleForTesting
    @Nullable
    public static String manageSmsBody(String body) {
        String code = getDigits(body);
        if (code != null) {
            BusHelper.post(new ValidationCodeEvent(code));
        }

        return code;
    }

    /**
     * Returns a string just with digits or null if there are no digits.
     */
    @Nullable
    public static String getDigits(String content) {
        if (content == null) {
            return null;
        }

        String code = content.replaceAll("\\D+", "");
        if (code.matches("\\d+")) {
            return code;
        } else {
            return null;
        }
    }
}

以上是关于java Android - BroadcastReceiver读取短信并发送带有代码读取的de.greenrobot.event.EventBus事件(消息应该只有代码的主要内容,如果未能解决你的问题,请参考以下文章

解决tensor.sub_(mean).div_(std)RuntimeError: output with shape [1, 28, 28] doesn‘t match the broadcas(

Android Cast Dialog - 暂停工作,不播放

wemall app商城源码Android短信监听接收器

Activity启动过程分析

四大组件—BroadcastContentProvider

四大组件—BroadcastContentProvider