【问题标题】:Huawei auto read sms华为自动阅读短信
【发布时间】:2021-07-13 15:23:25
【问题描述】:

我想在华为实现自动阅读短信。我引用了这个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 没有被调用。 请帮我解决这个问题

【问题讨论】:

    标签: android huawei-mobile-services huawei-account read-sms


    【解决方案1】:
    1. 先删除onPause方法。因为开启短信自动获取服务后,超时时间为5分钟。如果5分钟后触发onReceive方法,则广播功能开启成功。

      删除此方法

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

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

    prefix_flag short message verification code is XXXXXX hash_value
    

    1. 广播注册不能在manifest注册,只能在activity动态注册。

    【讨论】:

    • 感谢您的帮助。我按照你的建议做了。评论 public void onPause() { unregisterReceiver(smsBroadcast);并删除了清单广播部分。这可以。 " startSmsRetriever: isSuccessful" 但收到短信后仍然广播 onReceive 未调用。实际上,我从另一部手机生成所需的短信并将其发送到测试华为手机。我的意思是现在短信不是来自网络服务器。这里有问题吗?
    • hi@Thushara 1. SMS 消息不是来自 Web 服务器也可以。 2. 超过5分钟有没有收到超时广播? 2、您发送的消息格式是否符合要求?能否请您发布您发送的全文?
    • 顺便说一句,确保hash_value 字段正确。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 2010-10-28
      • 2016-06-19
      • 2013-04-17
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多