Android 华为自动阅读短信

Android 华为自动阅读短信,android,huawei-mobile-services,huawei-account,read-sms,Android,Huawei Mobile Services,Huawei Account,Read Sms,我想在华为实现自动读短信。我引用了这个 并根据需要设置所有内容。但广播不起作用。这是代码 舱单服务声明 <receiver android:name=".util.SMSBroadCastReceiver" android:exported="true"> <intent-filter> <action android:name="com.huawei.hms.support.

我想在华为实现自动读短信。我引用了这个 并根据需要设置所有内容。但广播不起作用。这是代码

舱单服务声明

<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>
我还生成了所需的哈希代码并发送短信

此处启动MSRetriever:isSuccessful。但我们没有接到电话。 请帮我做这个

  • 首先删除暂停时的
    onPause
    方法。因为启用自动短信获取服务后,超时时间为5分钟。如果
    onReceive
    方法在5分钟后触发,则广播功能成功启用

    删除此方法

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

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

  • 广播注册不能在
    清单
    中注册,只能在
    活动
    中动态注册

  • 谢谢你的帮助。我照你的建议做了。在Pause(){unregisterReceiver(smsBroadcast)上注释了public void;并删除了清单boradcast部分。这很好。“startSmsRetriever:isSuccessful”但在收到短信后,仍然没有呼叫广播onReceive。实际上,我从另一部手机生成了所需的短信,并将其发送到正在测试的华为手机。我的意思是,目前短信不是来自web服务器。这是一个问题吗?hi@Thushara1.短信不是来自网络服务器的,也可以。2.你有没有收到短信超过5分钟后不会广播?2.您发送的消息格式是否符合要求?请发布您发送的全文消息。顺便说一句,确保
    hash_值
    字段正确。
    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();
        }
        
    
    prefix_flag short message verification code is XXXXXX hash_value