回答
//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