Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取手机上一周内发送的所有当前短信_Java_Android_Post_Sms_Limit - Fatal编程技术网

Java 如何获取手机上一周内发送的所有当前短信

Java 如何获取手机上一周内发送的所有当前短信,java,android,post,sms,limit,Java,Android,Post,Sms,Limit,我正在制作一个应用程序,我需要在一周内将当前收到/发送的sms消息同步到我的web服务器。这是我的代码,但它会收到所有短信: SharedPreferences settings = getSharedPreferences("MyPrefs", 0); boolean firstTime = settings.getBoolean("FirstTime", true); if (firstTime) { Uri allMessage = Uri.parse("

我正在制作一个应用程序,我需要在一周内将当前收到/发送的sms消息同步到我的web服务器。这是我的代码,但它会收到所有短信:

 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秒)的消息

uriallmessage=Uri.parse(“content://sms/");
ContentResolver cr=getContentResolver();
游标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差=现在的时间-日期;
如果(差值<604800){
String phoneNumber=c.getString(2);
String message=c.getString(11);
//这条短信发布不到一周。
}
}

您需要为此使用选择标准

Cursor c = cr.query(allMessage, null, "recievedDate between date('now') and SELECT date('now','start of week','-1 week',''); ", null, null);
这将创建一个SQL查询来获取正确的daya。。您可能需要稍微调整一下选择条件

选择条件基本上是sql查询中的
where
子句,但
where
关键字除外

这里有一篇关于


希望这能有所帮助。

receivedDate是查询中的“发送日期”列吗?它实际上被称为“时间”,所以我会使用“日期之间的时间('now')并选择…”
Cursor c = cr.query(allMessage, null, "recievedDate between date('now') and SELECT date('now','start of week','-1 week',''); ", null, null);