如何获取手机当前一周内发送的所有短信
我正在制作一个应用程序,我需要将一周内接收/发送的当前短信同步到我的网络服务器。这是我的代码,但它获取了所有短信:
SharedPreferences settings = getSharedPreferences("MyPrefs", 0);
boolean firstTime = settings.getBoolean("FirstTime", true);
if (firstTime) {
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
//upload it all here
}
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FirstTime", false);
editor.commit();
}
另一个问题是,如果我一条一条地发送每条消息,处理起来会很麻烦吗?我是否应该将它们组合在一起,并将其转换为 XML/JSON 并对其进行编码(可能是 base64),然后将其发送到我的服务器?但我担心它可能会超出 HTTP POST 参数的发送限制。
编辑 我已经找到了解决方案,您需要使用查询返回的内容,因此第 5 列是所有消息的日期,因此您使用 if 语句仅提交 1 周内(604800 秒)的消息。
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while(c.moveToNext()){
int date = Math.round(Integer.parseInt(c.getString(4))/1000);
int time_now = Math.round(System.currentTimeMillis()/1000);
int difference = time_now - date;
if(difference < 604800){
String phoneNumber = c.getString(2);
String message = c.getString(11);
//this sms is within a week old.
}
}
I am making an application and I need to sync the current sms messages received/sent within a week to my web server. This is the code I have, but it gets all the SMS Messages:
SharedPreferences settings = getSharedPreferences("MyPrefs", 0);
boolean firstTime = settings.getBoolean("FirstTime", true);
if (firstTime) {
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
//upload it all here
}
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FirstTime", false);
editor.commit();
}
Another question is, will it be a lot to handle if I send each message one by one? Should I like group them together, and make it into XML/JSON and encode it (base64 probably), then send it to my server? But I'm afraid it might go over the sending limit of HTTP POST params.
EDIT
I have figured out the solution, you need to use what the query gives you back, so the 5th column is the date for all the messages, so you use an if statement to only submit the messages that are within 1 week (604800 seconds).
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while(c.moveToNext()){
int date = Math.round(Integer.parseInt(c.getString(4))/1000);
int time_now = Math.round(System.currentTimeMillis()/1000);
int difference = time_now - date;
if(difference < 604800){
String phoneNumber = c.getString(2);
String message = c.getString(11);
//this sms is within a week old.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要为此使用选择标准..
这将创建一个SQL查询来获取正确的daya..你可能需要稍微调整一下选择标准..
选择标准基本上是一个
where
SQL 查询中除where
关键字之外的子句。这是一篇关于 SQLLite 日期时间函数 的文章,
希望对您有所帮助。
you need to use the selection criteria for this..
this would create a SQL query to fetch the correct daya.. you might want to tweak the selection criteria a bit..
the selection criteria is basically a
where
clause in an sql query except for thewhere
keyword.here is an article on SQLLite datetime function
hope this helps.