Android 短信广播接收器,阅读短信
Android SMS broadcast receiver , Reading SMS
我的应用程序读取我通过第 3 方短信网关发送的 OTP 短信。它一直运行良好,我们在许多设备上进行了测试。然后是双卡MOTO E,即使收到OTP短信也无法通过验证。
对于双卡激活的双卡手机,读取 OTP 消息是否需要任何修改?
以下是我的短信接收器class:
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
private static String message;
Context myContext;
// public static final int recDigits= Integer.parseInt(message.substring(20,26));
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
context.unregisterReceiver(this);
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
if(LoginAuth.otpVerify == Integer.parseInt(message.substring(20,26).trim())){
Toast.makeText(context,"Sign Up Successful",Toast.LENGTH_LONG).show();
sendMessage();
}
/*intent.putExtra("msg", message.substring(20,26).trim());
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
message.substring(20,26), duration);
toast.show();*/
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(myContext).sendBroadcast(intent);
}
}
上面的 class 读取 OTP 消息,对 OTP 进行子字符串化并验证它。如果验证通过,用户将进入下一个屏幕。如果用户在 90 秒内没有收到 OTP,他将被要求再次提交。
现在,当我在 MOTO E dual sim 上进行测试时,手机会在 5-10 秒内生成并接收到 OTP,但我的 SMS 接收器不会验证它。
是双卡问题还是接收器问题?
我通过提供 google 文档中给出的过滤器优先级解决了上述问题。
private void registerSmsReciever() {
smsReciever = new IncomingSms();
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(2147483647);
this.registerReceiver(smsReciever, intentFilter);
}
我的应用程序读取我通过第 3 方短信网关发送的 OTP 短信。它一直运行良好,我们在许多设备上进行了测试。然后是双卡MOTO E,即使收到OTP短信也无法通过验证。
对于双卡激活的双卡手机,读取 OTP 消息是否需要任何修改?
以下是我的短信接收器class:
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
private static String message;
Context myContext;
// public static final int recDigits= Integer.parseInt(message.substring(20,26));
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
context.unregisterReceiver(this);
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
if(LoginAuth.otpVerify == Integer.parseInt(message.substring(20,26).trim())){
Toast.makeText(context,"Sign Up Successful",Toast.LENGTH_LONG).show();
sendMessage();
}
/*intent.putExtra("msg", message.substring(20,26).trim());
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
message.substring(20,26), duration);
toast.show();*/
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(myContext).sendBroadcast(intent);
}
}
上面的 class 读取 OTP 消息,对 OTP 进行子字符串化并验证它。如果验证通过,用户将进入下一个屏幕。如果用户在 90 秒内没有收到 OTP,他将被要求再次提交。
现在,当我在 MOTO E dual sim 上进行测试时,手机会在 5-10 秒内生成并接收到 OTP,但我的 SMS 接收器不会验证它。
是双卡问题还是接收器问题?
我通过提供 google 文档中给出的过滤器优先级解决了上述问题。
private void registerSmsReciever() {
smsReciever = new IncomingSms();
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(2147483647);
this.registerReceiver(smsReciever, intentFilter);
}