Android 安卓系统中短信的自动检测
我正在尝试从短信自动检测OTP,并在EditText中填写OTP。我试图通过一个接收者服务将其存档,但它将日志作为“it's null”提供,而不是自动填充EditText(ETLASTTP) 这是我的服务课:Android 安卓系统中短信的自动检测,android,broadcastreceiver,sms,android-service,one-time-password,Android,Broadcastreceiver,Sms,Android Service,One Time Password,我正在尝试从短信自动检测OTP,并在EditText中填写OTP。我试图通过一个接收者服务将其存档,但它将日志作为“it's null”提供,而不是自动填充EditText(ETLASTTP) 这是我的服务课: import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreference
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.SmsMessage;
import android.util.Log;
public class SmsListener extends BroadcastReceiver {
public OnSmsReceivedListener listener = null;
public Context context;
public SmsListener()
{
}
public void setOnSmsReceivedListener(Context context) {
Log.d("Listener","SET");
this.listener = (OnSmsReceivedListener) context;
Log.d("Listener","SET SUCCESS");
}
public interface OnSmsReceivedListener {
public void onSmsReceived(String otp);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
Log.d("MsgBody", msgBody);
String otpSMS=msgBody.substring(36,36+4);
if (listener != null) {
listener.onSmsReceived(otpSMS);
}
else
{
Log.d("Listener", "Its null");
}
}
}catch(Exception e){
Log.d("Exception caught", e.getMessage());
}
}
}
}
}
您没有显示任何注册您的
BroadcastReceiver
的代码,因此我要冒险一试,猜测您在BroadcastReceiver
的清单条目中添加了一个
,该条目在收到SMS时触发。这可以解释你的问题
如果您的BroadcastReceiver
有一个
,收到短信后,Android会自动为您的BroadcastReceiver
创建一个新实例,并在该新实例上调用onReceive()
。这个实例,因为它是由Android创建的,所以没有设置侦听器
要解决此问题,您需要从清单中删除
,并且需要在创建BroadcastReceiver
后动态注册它。在活动
中,在创建广播接收器
后,调用registerReceiver()
。您需要创建一个IntentFilter
,描述您想要接收的广播Intent
。您可以使用我的简单库
您需要重写接口方法,创建拦截器类并将其与生命周期绑定:
public class AuthActivity extends AppCompatActivity implements OnMessageListener {
private SmsInterceptor smsInterceptor;
private EditText etAuthPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
initViews();
initSmsInterceptor();
}
@Override
protected void onResume() {
super.onResume();
smsInterceptor.register();
}
@Override
protected void onPause() {
super.onPause();
smsInterceptor.unregister();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsInterceptor.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void messageReceived(String message) {
// You can perform your validation here
etAuthPassword.setText(message);
}
private void initViews() {
etAuthPassword = findViewById(R.id.etAuthPassword);
etAuthPassword.addTextChangedListener(new SmsTextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 4) {
btnAuthPassword.setEnabled(true);
checkCode();
} else {
btnAuthPassword.setEnabled(false);
}
}
});
}
private void initSmsInterceptor() {
smsInterceptor = new SmsInterceptor(this, this);
// Not necessary
smsInterceptor.setRegex(SMS_CODE_REGEX);
smsInterceptor.setPhoneNumber(PHONE_NUMBER);
}
private void checkCode() {
// Validation
if (isValid) {
navigateToMainScreen();
}
}
}
你不能直接写40而不是36+4吗?@VivekMishra-将其改为40,但这不是问题所在。我知道,但试着把代码写得尽可能简单易懂,让别人更容易理解understand@VivekMishra-好的,谢谢。:)不要使用接口,而是尝试将otp的值保存在一个静态字符串中,并使用类nameBravo使用它!你救了我一天!这成功了!谢谢你,大卫。
public class AuthActivity extends AppCompatActivity implements OnMessageListener {
private SmsInterceptor smsInterceptor;
private EditText etAuthPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
initViews();
initSmsInterceptor();
}
@Override
protected void onResume() {
super.onResume();
smsInterceptor.register();
}
@Override
protected void onPause() {
super.onPause();
smsInterceptor.unregister();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsInterceptor.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void messageReceived(String message) {
// You can perform your validation here
etAuthPassword.setText(message);
}
private void initViews() {
etAuthPassword = findViewById(R.id.etAuthPassword);
etAuthPassword.addTextChangedListener(new SmsTextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 4) {
btnAuthPassword.setEnabled(true);
checkCode();
} else {
btnAuthPassword.setEnabled(false);
}
}
});
}
private void initSmsInterceptor() {
smsInterceptor = new SmsInterceptor(this, this);
// Not necessary
smsInterceptor.setRegex(SMS_CODE_REGEX);
smsInterceptor.setPhoneNumber(PHONE_NUMBER);
}
private void checkCode() {
// Validation
if (isValid) {
navigateToMainScreen();
}
}
}