reading sms automatically using broad cast reciver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SmsReceiver extends BroadcastReceiver {
private static SmsListener mListener;
Boolean b;
String abcd,xyz;
private static String TAG = "";
@Override
public void onReceive(Context context, Intent intent) {
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 += smsm[i].getMessageBody().toString();
String Sender = smsm[i].getOriginatingAddress();
//Check here sender is yours
Intent smsIntent = new Intent("otp");
String pattern = String.valueOf(Pattern.compile( "(\\d{4})"));
Log.d(TAG, "onReceive: "+pattern);
// Create a Pattern object
Pattern filter = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = filter.matcher(sms_str);
Log.d(TAG, "onReceive: filtered " + m);
smsIntent.putExtra("message",sms_str);
Log.d(TAG, "onReceive: " + sms_str);
LocalBroadcastManager.getInstance(context).sendBroadcast(smsIntent);
}
}
}
// methd for communication sending sms to actvity
public static void bindListener(SmsListener listener) {
mListener = listener;
}
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("otp")) {
final String message = intent.getStringExtra("message");
// editTextOtp.setText(message);
// firstDigitOtpEdt.setText(message);
myString = message;
String[] digitsArray = myString.split("");
firstDigitOtpEdt.setText(digitsArray[1]);
secondDigitOtpEdt.setText(digitsArray[2]);
thirdDigitOtpEdt.setText(digitsArray[3]);
fourthDigitOtpEdt.setText(digitsArray[4]);
//Do whatever you want with the code here
}
}
};
@Override
public void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("otp"));
super.onResume();
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
onCreate(Bundle s) {
checkPermissions();
}
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
&& (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED)) {
receiveOtp();
} else {
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST);
}
} else {
receiveOtp();
}
}
// recving otp from broadcast reciver
private void receiveOtp() {
SmsReceiver.bindListener(new SmsListener() {
@Override
public void messageReceived(String messageText) {
myString = messageText;
}
});
}
//result of runtime permission
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST) {
if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Please Enable sms read and recieve permissions for auto OTP", Toast.LENGTH_LONG).show();
} else {
receiveOtp();
}
}
}
public interface SmsListener {
public void messageReceived(String messageText);
}
// interface for communicating otp
<LinearLayout
android:id="@+id/otpView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">
<EditText
android:id="@+id/et1"
android:layout_width="50dp"
android:layout_height="50dp"
android:inputType="number"
android:maxLength="1"
android:textAlignment="center"
android:theme="@style/Theme.App.Base" />
<android.support.v4.widget.Space
android:layout_width="2dp"
android:layout_height="0dp" />
<EditText
android:id="@+id/et2"
android:layout_width="50dp"
android:layout_height="50dp"
android:inputType="number"
android:maxLength="1"
android:textAlignment="center"
android:theme="@style/Theme.App.Base" />
<android.support.v4.widget.Space
android:layout_width="2dp"
android:layout_height="0dp" />
<EditText
android:id="@+id/et3"
android:layout_width="50dp"
android:layout_height="50dp"
android:inputType="number"
android:maxLength="1"
android:textAlignment="center"
android:theme="@style/Theme.App.Base" />
<android.support.v4.widget.Space
android:layout_width="2dp"
android:layout_height="0dp" />
<EditText
android:id="@+id/et4"
android:layout_width="50dp"
android:layout_height="50dp"
android:inputType="number"
android:maxLength="1"
android:textAlignment="center"
android:theme="@style/Theme.App.Base" />
<android.support.v4.widget.Space
android:layout_width="2dp"
android:layout_height="0dp" />
</LinearLayout>