0

I am developing an android application in which I want to read the sms and pass the data to my application.

I tried many ways but cant't figure it out. Here is what I am doing. Is something wrong in my code.

 String msg_body = "";
       Uri my_uri = Uri.parse("content://sms/inbox");
       Cursor readFstSms = v.getContext().getContentResolver().query(my_uri, null, null, null, null);
       if(readFstSms.moveToFirst())
       {
           msg_body = readFstSms.getString(readFstSms.getColumnIndexOrThrow("body")).toString();
       }
       readFstSms.close();
       Uri gmmIntentUri = Uri.parse("google.navigation:q=" + msg_body);
       Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
       mapIntent.setPackage("com.google.android.apps.maps");
       startActivity(mapIntent);

I also added <uses-permission android:name="android.permission.READ_SMS"/> in my manifest file but still can't find the solution

Vaibhav More
  • 994
  • 3
  • 10
  • 22
  • Have a look here: http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it. If that's not the issue, you need to provide a lot more detail as to how exactly it's not working. – Mike M. Jan 21 '17 at 19:02
  • Thanks a lot!! But still it is not solving problem. Can anyone help me with this problem. I want to deploy this app – Vaibhav More Jan 21 '17 at 20:20
  • Help you with what? You haven't even said what the problem is. Does it not read what you expect it to? Does it not read anything? Does it not start the `Activity`? Does it crash? Does your phone explode? What? "cant't figure it out" is not an adequate problem statement. – Mike M. Jan 21 '17 at 21:00
  • It does not store my message body which I need to retrive from message. – Vaibhav More Jan 21 '17 at 21:10

1 Answers1

0

First of all you need to set the read permissions for the app through manifest

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

And you need a BroadcastReciever which should be declared in manifest with the intent filter android.provider.Telephony.SMS_RECEIVED '
Once the reciever starts working, You can handle the recieved SMS in the onRecieve callback method of that BroadcastReceiver.

if (intentExtras != null) {
            Object[] messages = (Object[]) intentExtras.get(SMS_BUNDLE);

            for (int i = 0; i < messages.length; ++i) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) messages[i]);

                String body = sms.getMessageBody().toString();

 Toast.makeText(context, body, Toast.LENGTH_SHORT).show();

            }
ravip
  • 91
  • 1
  • 6