问题描述
我想在华为实现自动读取短信。我参考了这个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 没有被调用。 请帮我解决这个问题
解决方法
-
先删除
onPause
方法。因为开通短信自动获取服务后,超时时间为5分钟。如果onReceive
方法在5分钟后触发,则广播功能开启成功。删除这个方法
五分钟后,检查
onReceive
方法是否被触发。如果是,则广播正常。 -
SMS 消息格式必须符合以下示例。如果短信格式不正确,即使手机收到短信也不会触发广播。广播在 5 分钟后超时。
prefix_flag short message verification code is XXXXXX hash_value
- 广播注册不能在
manifest
中注册,只能在activity
中动态注册。
我在应用程序中更新了 HMS 核心并重新启动设备,它神奇地工作了。