I want to read the message body of a new incoming SMS in android, programmatically.
I tried something but that doesn't return any contents:
Uri uri = Uri.parse("content://sms/inbox"); ContextWrapper context = null; Cursor c = context.getContentResolver().query(uri, null, null ,null,null); String body = null; String number=null; if(c.moveToFirst()) { body = c.getString(c.getColumnIndexOrThrow("body")).toString(); number = c.getString(c.getColumnIndexOrThrow("address")).toString(); } c.close();
In the “Notification Access” menu that appears, tap the toggle next to “Google.” Tap “Allow” in the window that appears to grant Google access. Head back to Google Assistant or say, “OK/Hey, Google,” again, and then repeat the, “Read my text messages,” instruction.
Read your messages On your Android phone, touch and hold the Home button or say "Hey Google." Say, “read my messages.”
Before starting your application, Android studio installer will display following window to select an option where you want to run your Android application. Now you can enter a desired mobile number and a text message to be sent on that number. Finally click on Send SMS button to send your SMS.
I have posted some sample programs about this on my class website. Here is the example Read SMS Example Here is a snippet of code. Basically your can register a broadcast receiver to listen for SMS_Receive and check out the following.
Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("mySMS"); if (bundle != null) { Object[] pdus = (Object[])bundle.get("pdus"); SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]); Log.i("mobile.cs.fsu.edu", "smsActivity : SMS is <" + sms.getMessageBody() +">"); //strip flag String message = sms.getMessageBody(); while (message.contains("FLAG")) message = message.replace("FLAG", ""); TextView tx = (TextView) findViewById(R.id.TextBox); tx.setText(message); } else Log.i("mobile.cs.fsu.edu", "smsActivity : NULL SMS bundle");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With