2013-05-06 47 views
0

嗨我希望從我的應用程序能夠從手機上的聯繫人導入短信並將其轉換爲字符串。我想知道這是否可能以任何方式?我試圖尋找答案,但似乎沒有找到任何。希望有人能幫助我吧:)提前從手機導入消息到應用程序

感謝:d

+0

你是什麼意思的「一條消息」? 短信?彩信?特定於應用的事件消息? – 2013-05-06 12:10:40

+0

來自手機上的聯繫人的短信 – 2013-05-06 12:12:45

+0

我可以看到爲什麼你會問這個問題,因爲它依賴於使用非文檔內容提供者('content:// sms /'),但是有很多例子可以說明如何要做到這一點 – 2013-05-06 12:19:00

回答

0
//this class will hold our sms information 
public class Sms 
{ 
    public String Id; 
    public String Address; 
    public String Readstate; 
    public String Message; 
    public String Time; 

    public Sms(string id, string address, string message, string readstate, string time) 
    { 
     Id = id; 
     Address = address; 
     Message = message; 
     Readstate = readstate; 
     Time = time; 
    } 
} 

該功能可以retreive用戶的手機上的文件夾中的所有短信。

的文件夾是隻是基本的那些與短信打交道時,你會期望(「收件箱」,「已發送」,等等)

//gets all sms messages in a specific folder in the user's sms messages 
public List<Sms> getAllSms(String folderName) 
{ 
    //initiate a new ArrayList to put our messages in 
    //ArrayLists are basically arrays on steroids (this is basic Java stuff) 
    List<Sms> lstSms = new ArrayList<Sms>(); 

    //The SMS object is somewhere in the Android SDK. 
    //your IDE should be able to resolve where to find it for you. 
    Sms objSms = new Sms(); 

    //find the SMS messages on the phone in the directory we want 
    //using android's content resolver 
    Uri message = Uri.parse("content://sms/"+folderName); 
    ContentResolver cr = mActivity.getContentResolver(); 

    //initiate a Cursor object that will help us iterate through the result set 
    Cursor c = cr.query(message, null, null, null, null); 
    mActivity.startManagingCursor(c); 
    int totalSMS = c.getCount(); 

    //if we can find a message in this result set: 
    if (c.moveToFirst()) { 
     //iterate through all the messages in our result set 
     for (int i = 0; i < totalSMS; i++) { 
      //retrieve the contents of this message and put them in "our" Sms object 
      objSms = new Sms(
       c.getString(c.getColumnIndexOrThrow("_id")), //retrieve Id, crash if Id cannot be found 
       c.getString(c.getColumnIndexOrThrow("address")), //retreive address, crash if it cannot be found 
       c.getString(c.getColumnIndexOrThrow("body")), //retreive message content, crash if it cannot be found 
       c.getString(c.getColumnIndex("read")), //retreive whether message is read or not 
       c.getString(c.getColumnIndexOrThrow("date")) //retreive message date, crash if it cannot be found 
      ); 
      lstSms.add(objSms); 
      c.moveToNext(); 
     } 
    } 
    //optionally, you can uncomment the following code to have error handling 
    //for empty sms folders: 
    // else { 
    // throw new RuntimeException("You have no SMS in " + folderName); 
    // } 

    //close the cursor and free up resources 
    c.close(); 

    //return the sms files we found in the directory 
    return lstSms; 
} 

然後,您可以retreive這樣的消息:

List<Sms> inboxMessages = getAllSms("inbox"); // Get all sms from inbox 
    List<Sms> sentMessages = getAllSms("sent"); // Get all sms from sent 
+0

請注意,這些示例中的短信內容提供商沒有記錄。你的旅費可能會改變。 – 2013-05-06 12:19:25

+0

我不完全確定上面的代碼是如何工作的(對於編程有點新穎),你可以試着解釋一下嗎? :) – 2013-05-06 12:25:16

+0

此外,它看起來像它會發現所有的短信,但我正在尋找一種方法,讓用戶選擇他/她想要導入和使用在APP中的短信。 – 2013-05-06 12:27:25

相關問題