Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the message content of a new in coming message in android?

Tags:

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();  
like image 205
Krishna Avatar asked Feb 15 '11 17:02

Krishna


People also ask

How do I read incoming messages on Android?

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.

How do I read a message?

Read your messages On your Android phone, touch and hold the Home button or say "Hey Google." Say, “read my messages.”

How can Android instant messaging send and listen SMS?

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.


1 Answers

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"); 
like image 148
Frank Sposaro Avatar answered Sep 28 '22 11:09

Frank Sposaro