Я пытаюсь использовать API Google Retriever для автоматической проверки SMS.Я следовал инструкциям здесь , но мое приложение не получает SMS-сообщений.Я пробовал много способов, но это все еще не работает, и я действительно не понимаю, почему.
Вот что я сделал.Сначала я создаю класс MySMSBroadcastReceiver
:
public class MySMSBroadcastReceiver extends BroadcastReceiver {
public OTPReceiveListener otpReceiveListener;
public void initOtpReceiveListener(OTPReceiveListener otpReceiveListener) {
this.otpReceiveListener = otpReceiveListener;
}
@Override
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch(status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
otpReceiveListener.onOTPReceived(message);
break;
case CommonStatusCodes.TIMEOUT:
otpReceiveListener.onOTPTimeOut();
break;
}
}
}
public interface OTPReceiveListener {
void onOTPReceived(String otp);
void onOTPTimeOut();
}
}
Для класса OTPActivity
я удаляю несвязанный код:
public class OTPActivity extends BaseActivity implements IDelegateResponse<OTPResponse>, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, MySMSBroadcastReceiver.OTPReceiveListener {
@BindView(R.id.edtSmsOtp)
EditText edtSmsOtp;
private final String TAG = "OTPActivity";
private MySMSBroadcastReceiver mySMSBroadcastReceiver;
@Override
public int getLayoutID() {
return R.layout.activity_otp;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpOTPSms();
}
private void setUpOTPSms() {
AppSignatureHelper appSignatureHelper = new AppSignatureHelper(OTPActivity.this);
Utils.showLog(TAG, "getAppSignatures:" + appSignatureHelper.getAppSignatures());
mySMSBroadcastReceiver = new MySMSBroadcastReceiver();
mySMSBroadcastReceiver.initOtpReceiveListener(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);
intentFilter.setPriority(2147483647);
registerReceiver(mySMSBroadcastReceiver, intentFilter);
startSMSListener();
}
private void startSMSListener() {
SmsRetrieverClient client = SmsRetriever.getClient(OTPActivity.this);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(aVoid -> {
//Toast.makeText(OTPActivity.this, "SMS Retriever starts", Toast.LENGTH_LONG).show();
Utils.showLog(TAG, "SMS Retriever starts");
});
task.addOnFailureListener(e -> Toast.makeText(OTPActivity.this, "Error", Toast.LENGTH_LONG).show());
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onOTPReceived(String otp) {
if (mySMSBroadcastReceiver != null) {
unregisterReceiver(mySMSBroadcastReceiver);
}
Utils.showLog("OTP Received", "OTP: " + otp);
}
@Override
public void onOTPTimeOut() {
Utils.showLog("OTP Received", "onOTPTimeOut");
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mySMSBroadcastReceiver != null) {
unregisterReceiver(mySMSBroadcastReceiver);
}
}
}