2012-07-09 102 views
1

嗨,我是新的android應用程序開發,並且我在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"); 

    } 
} 
+0

提高你的accepte率 – 2012-07-09 09:38:06

+3

@SamirMangroliya:真的嗎? ](http://meta.stackexchange.com/questions/23321/is-it-closed-to-comment-on-peoples-accept-rate) – Bobby 2012-07-09 09:39:27

+0

我不明白......你說什麼? – cheliyan 2012-07-09 09:41:18

回答

0

如果要擴展DroidGap,沒必要做的setContentView中的onCreate,DroidGap會默認顯示web視圖。


創建一類說具有方法來讀取未讀的SMS和添加ReadSMS類對象作爲javascriptInterfcae到droidGap web視圖,如下ReadSMS:

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> 
+0

要知道,如何閱讀未讀的短信,請參閱:http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android – 2012-07-09 10:01:15

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(); 
} 
相關問題