In my application i am using SMSbroadcastreciever but when i am running app on lollipop or greater version it works but when i am running on lower version it sms cannot read automatically. when i am going in permission of app in device the sms recieve not granted in kitkat even i have given runtime permission in app and also in manifest file
in other version code works fine:
Here is my code:
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);
}
}
}
};