回答
//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
請注意,這些示例中的短信內容提供商沒有記錄。你的旅費可能會改變。 – 2013-05-06 12:19:25
我不完全確定上面的代碼是如何工作的(對於編程有點新穎),你可以試着解釋一下嗎? :) – 2013-05-06 12:25:16
此外,它看起來像它會發現所有的短信,但我正在尋找一種方法,讓用戶選擇他/她想要導入和使用在APP中的短信。 – 2013-05-06 12:27:25
- 1. Kony:從手機導入圖像到應用程序
- 2. Win32 GUI應用程序:如何將stderr消息導入到消息箱中
- 3. 消息從打包的應用程序傳遞到分機
- 4. 如何在從內置消息應用程序發送消息後意圖到本機應用程序
- 5. 發送ems消息從pc到手機
- 6. Android的C2DM消息到達手機,轉發到錯誤的應用程序
- 7. 從PC發送消息到手機c#應用
- 8. 從手機通訊錄導入到我的應用程序在iPhone
- 9. 從Java應用程序向員工手機發送免費消息
- 10. 使用Codeigniter發送消息到手機
- 11. 從aosp構建手機應用程序
- 12. 限制應用程序到手機
- 13. 開發應用程序到手機
- 14. Facebook到手機應用程序
- 15. 與「無消息」從Spring應用程序
- 16. 從iPhone應用程序發送消息
- 17. Android手機應用程序
- 18. Windows手機應用程序
- 19. GCM應用程序未收到消息
- 20. Python消息到其他應用程序
- 21. 將消息拉到Android應用程序
- 22. WP8 Facebook發送消息應用程序到應用程序
- 23. 手機應用程序,從瀏覽器到手機上的應用程序的重定向
- 24. 如何從應用程序中將信息保存並加載到手機中?
- 25. 如何發送消息從c#表單應用程序到Socket.io應用程序
- 26. 從Java應用程序發送消息到偵聽的Android應用程序
- 27. 如何將消息從Win32應用程序發送到Java SWT應用程序?
- 28. 從手機應用程序發送變量值到另一個手機實時
- 29. 將視頻嵌入到手機應用程序
- 30. 在Android手機上找不到從AIR應用程序導出的圖像
你是什麼意思的「一條消息」? 短信?彩信?特定於應用的事件消息? – 2013-05-06 12:10:40
來自手機上的聯繫人的短信 – 2013-05-06 12:12:45
我可以看到爲什麼你會問這個問題,因爲它依賴於使用非文檔內容提供者('content:// sms /'),但是有很多例子可以說明如何要做到這一點 – 2013-05-06 12:19:00