在我的应用程序中,我正在使用SMSbroadcastreciever,但是当我在棒棒糖或更高版本上运行应用程序时,它可以工作,但是当我在较低版本上运行时,它无法自动读取短信。 当我在设备中获得应用程序的权限时,即使我已经在应用程序中以及清单文件中提供了运行时权限,也没有在kitkat中获得短信接收
其他版本代码中的工作正常:
这是我的代码:
public class MySMSBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Get Bundle object contained in the SMS intent passed in
Bundle bundle = intent.getExtras();
SmsMessage[] smsm = null;
String sms_str = "";
if (bundle != null) {
// Get the SMS message
Object[] pdus = (Object[]) bundle.get("pdus");
smsm = new SmsMessage[pdus.length];
for (int i = 0; i < smsm.length; i++) {
smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sms_str += "\r\nMessage: ";
sms_str += smsm[i].getMessageBody().toString();
sms_str += "\r\n";
sms_str = sms_str.replaceAll("[^0-9?!\\.]", "");
String Sender = smsm[i].getOriginatingAddress();
//Check here sender is yours
Intent smsIntent = new Intent("otp");
smsIntent.putExtra("message", sms_str);
LocalBroadcastManager.getInstance(context).sendBroadcast(smsIntent);
}
}
}
}
<receiver android:name=".Otpsms.MySMSBroadCastReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
MySMSBroadCastReceiver receiver = new MySMSBroadCastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("otp")) {
final String message = intent.getStringExtra("message");
if (et_code_otp != null) {
et_code_otp.setText(message);
}
}
}
};