2

我想在华为实现自动阅读短信。我参考了这个https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/readsmsmanager-0000001050050861-V5 并根据需要设置所有内容。但广播不起作用。这是代码。

清单服务声明

<receiver
    android:name=".util.SMSBroadCastReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.huawei.hms.support.sms.common.ReadSmsConstant.READ_SMS_BROADCAST_ACTION" />
    </intent-filter>
</receiver>

广播类

public class SMSBroadCastReceiver extends BroadcastReceiver {

    private static final String TAG = "SMSBroadCastReceiver";
    private OTPReceiveListener otpReceiver = null;

    public void initOTPListener(OTPReceiveListener receiver) {
        this.otpReceiver = receiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null && ReadSmsConstant.READ_SMS_BROADCAST_ACTION.equals(intent.getAction())) {
            Status status = bundle.getParcelable(ReadSmsConstant.EXTRA_STATUS);
            if (status.getStatusCode() == CommonStatusCodes.TIMEOUT) {
                // The service has timed out and no SMS message that meets the requirements is read. The service process ends.
                Log.i(TAG, "onReceive: TIMEOUT ");
                this.otpReceiver.onOTPTimeOut();
            } else if (status.getStatusCode() == CommonStatusCodes.SUCCESS) {
                if (bundle.containsKey(ReadSmsConstant.EXTRA_SMS_MESSAGE)) {
                    // An SMS message that meets the requirement is read. The service process ends.
                    Log.i(TAG, "onReceive: received " + bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE));
                    this.otpReceiver.onOTPReceived(bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE));
                }
            }
        }
    }

    public void startSmsRetriever(Context context) {
        Task<Void> task = ReadSmsManager.start(context);
        task.addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    // The service is enabled successfully. Perform other operations as needed.
//                    doSomethingWhenTaskSuccess();
                    Log.i(TAG, "startSmsRetriever: isSuccessful");
                }else{
                    //task false
                    Log.i(TAG, "startSmsRetriever: failed");
                }
            }
        });
    }

    public interface OTPReceiveListener {

        void onOTPReceived(String otp);

        void onOTPTimeOut();
    }
}

活动类别代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        smsBroadcast = new SMSBroadCastReceiver();
        smsBroadcast.initOTPListener(this);
        smsBroadcast.startSmsRetriever(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ReadSmsConstant.READ_SMS_BROADCAST_ACTION);
        registerReceiver(smsBroadcast, intentFilter);
    }

    @Override
    public void onPause() {
        unregisterReceiver(smsBroadcast);
        super.onPause();
    }
    

我还生成了所需的哈希码并发送了短信。

这里 startSmsRetriever: isSuccessful。但是广播 onReceive 没有被调用。请帮助我

4

2 回答 2

0

我在应用程序中更新了 HMS 核心并重新启动设备,它神奇地工作了。

于 2021-07-13T07:23:25.770 回答
0
  1. 先删除onPause方法。因为开启短信自动获取服务后,超时时间为5分钟。如果onReceive5分钟后触发该方法,则广播功能开启成功。

    删除此方法

    在此处输入图像描述

    五分钟后,检查该onReceive方法是否被触发。如果是,则广播正常。

    在此处输入图像描述

  2. SMS 消息格式必须符合以下示例。如果短信格式不正确,即使手机收到短信也不会触发广播。广播在 5 分钟后超时。

prefix_flag short message verification code is XXXXXX hash_value

在此处输入图像描述

  1. 广播注册不能在中注册manifest,只能在中动态注册activity
于 2021-04-22T07:34:56.417 回答