如何以编程方式从android中的收件箱中读取未读短信?

时间:2012-07-09 09:37:31

标签: java android sms

嗨,我是Android应用程序开发的新手,我在android中开发了一个简单的应用程序。在我的应用程序中,我必须添加一个新功能。

我想从android的收件箱中读取短信。我搜索并应用了一些代码但没有用,我没有得到结果。以下代码是我的第一个活动代码。它将执行并转到索引页面并正常工作。在这种情况下,我想从收件箱中读取未读短信。在哪里我想编写代码以及我想编写哪些类型的代码....请非常明确地给我答案为什么因为我是一个非常新的android。

所以请帮助我。提前谢谢。

代码:

 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");

    }
}

2 个答案:

答案 0 :(得分:4)

它真的有用......用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);
}

}

而不是在主要活动中使用此方法调用

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

答案 1 :(得分:0)

如果您正在扩展DroidGap,无需在onCreate中执行setContentView,DroidGap将默认显示webView。

<小时/> 创建一个类,说ReadSMS有读取未读短信的方法,并将ReadSMS类对象作为javascriptInterfcae添加到droidGap webview,如下所示:

Class ReadSMS{

public ReadSMS(){
}

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

}

<小时/> 在SMSActivity的onCreate中添加:

ReadSMS readSms = new ReadSMS();
appView.addJavascriptInterface(readSms , "ReadUnreadSMS");
super.loadUrl("file:///android_asset/www/index.html");

<小时/> 然后在Javascript中,您可以在ReadSMS类中访问readUnreadSMS(),如下所示:

<script>
  $(function(){
        var smsArray[] = window.ReadUnreadSMS.readUnreadSMS();
  });
</script>