I have been using these code to receive sms in actual device android 4.2 and it was working fine, Now I came to know about new permission module so this code in android 7 nougat not working for me.
public class sms extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
Object[] smsExtra = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[smsExtra.length];
for (int i = 0; i < msgs.length; i++) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
String body = sms.getMessageBody().toString();
String sender = sms.getOriginatingAddress().toString();
Toast.makeText(context, "From :"+sender+"\n"+"body:"+body, Toast.LENGTH_LONG).show();
}
}
}
}
I found this about marshmallow that there is run time permission required, But i don't get it how and where to add it in my code so that it work in Nougat and below apis.
// in manifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />