In my app I want to receive a verification code of specific phone number. after reception the intent needs to be passed to other activity. Guide me how I can do it, and tell me where's the problem. If you can, teach me that how I can receive sms and read that sms of a specific phone number by simple code. or introduce an app with source code that has this sort of activity.
public class MySMSBroadCastReceiver extends BroadcastReceiver {
String specificPhoneNumber = "+989033001001";//phNum .Sender
@Override
public void onReceive(Context context, Intent intent) {
// Get Bundle object contained in the SMS intent passed in
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] smsm = null;
String sms_str = "";
if (bundle != null) {
// Get the SMS message
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus"); //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();
if (specificPhoneNumber.equals(Sender)) {
Uri uri = Uri.parse("content://sms/inbox");
ContentResolver contentResolver = context.getContentResolver();
String where = "address="+Sender;
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
null);
while (cursor.moveToNext()) {
long thread_id = cursor.getLong(1);
where = "thread_id="+thread_id;
Uri thread = Uri.parse("content://sms/inbox");
context.getContentResolver().delete(thread, where, null);
}
if (sms_str == "333") {
Toast.makeText(context,"тест", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, sms_str, Toast.LENGTH_LONG).show();
}
Intent l = new Intent(context,SmsReport.class);
l.putExtra("msg",sms_str); //Transfer Data Between Activities Using Intent
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
abortBroadcast();
}
}
}
}
}