1

Hi, I am new for android application development, and I have developed one simple application in android. In my application I have to add a new feature.

I want to read sms messages from the inbox in android. I have searched and applied some codes but no use, I don't get the result. Following code is my first activity code. It will execute and go to the index page and is working properly. In this case what I want to do for read unread sms from inbox. Where I want to write code and which types of code I want to write....please give me the answer very clearly why because am a very new for android.

So please help me. Thanks in advance.

Code:

 import org.apache.cordova.*;  
 import android.os.Bundle;
 public class SMSActivity extends DroidGap 
 {
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       super.setIntegerProperty("splashscreen", R.drawable.fg_logo);
       super.loadUrl("file:///android_asset/www/index.html");

    }
}
Spectre87
  • 2,374
  • 1
  • 24
  • 37
cheliyan
  • 1,679
  • 3
  • 16
  • 22
  • 3
    @SamirMangroliya: [Oh really?](http://meta.stackexchange.com/questions/23321/is-it-appropriate-to-comment-on-peoples-accept-rate) – Bobby Jul 09 '12 at 09:39
  • i can't understand...what u said? – cheliyan Jul 09 '12 at 09:41
  • try this http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android?rq=1 – Ran Jul 09 '12 at 09:44
  • i have seen this already. but i dont know how to implement in my application.please help me – cheliyan Jul 09 '12 at 09:50
  • I don't see any relevance of your question with your code. This is so stupid question, it like asking I have got all four wheels how can I build a car. – Ishaan Jun 26 '16 at 11:10

2 Answers2

4

Its Really Work... save with Message.java

package mzsay.com.provider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
public class Message {



final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
@SuppressWarnings("unused")
private ContentResolver resolver;

public Message(ContentResolver ConResolver){
    resolver = ConResolver;
}

public String getMessage(int batas) {
      Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
      String sms = "Message >> \n";
      int hitung = 0;
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
          if(hitung == batas)
              break;
          hitung++;
      }
      return sms;
}

public int getMessageCountUnread(){
    Cursor c = resolver.query(SMS_INBOX, null, "read = 0", null, null);
    int unreadMessagesCount = c.getCount();
    c.deactivate();
    return unreadMessagesCount;
}

public String getMessageAll(){
      Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
      String sms = "Message >> \n";
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
      }
      return sms;
}

public String getMessageUnread() {
    Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
      String sms = "Message >> \n";
      int hitung = 0;
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
          if(hitung == getMessageCountUnread())
              break;
          hitung++;
      }
      return sms;
}

public void setMessageStatusRead() {
    ContentValues values = new ContentValues();
    values.put("read",true);
    resolver.update(SMS_INBOX,values, "_id="+SmsMessageId, null);
}

}

And than call with this method in your Main Activity

public synchronized String getMessage(int batas) {
    Message ambilpesan = new Message(context.getContentResolver());
            //this a return of All unread from your Inbox
    return ambilpesan.getMessageUnread();
}
mzsay
  • 41
  • 2
0

If you are extending DroidGap, no need to do setContentView in onCreate, DroidGap will display webView by default.


Create a Class say ReadSMS having method to read unread sms and add ReadSMS class object as a javascriptInterfcae to droidGap webview, as below:
Class ReadSMS{

public ReadSMS(){
}

public String[] readUnreadSMS(){
return smsArray;
}

}


In SMSActivity's onCreate add:
ReadSMS readSms = new ReadSMS();
appView.addJavascriptInterface(readSms , "ReadUnreadSMS");
super.loadUrl("file:///android_asset/www/index.html");


Then in Javascript, you can access readUnreadSMS() in ReadSMS class as below:
<script>
  $(function(){
        var smsArray[] = window.ReadUnreadSMS.readUnreadSMS();
  });
</script>
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Swathi EP
  • 3,864
  • 6
  • 26
  • 25
  • To know, how to read unread sms, refer: http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android – Swathi EP Jul 09 '12 at 10:01