如何在具有特定时间的android中读取用户发送的消息

时间:2017-04-24 12:19:30

标签: android cursor sms

我创建了一个服务,每隔15分钟读取用户消息,代码对所有消息都正常工作但问题是我想读取最近15分钟发送的消息而不是所有消息,这是我的服务代码< / p>

public class MessageReadingService extends Service {
     ScheduledExecutorService scheduler =   Executors.newSingleThreadScheduledExecutor();
int happy,sad,lonely,joyful=0;
Cursor cur;

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    scheduler.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {

            Uri uriSMSURI = Uri.parse("content://sms/sent");

                 cur = getContentResolver().query(uriSMSURI, new String[]{"_id", "thread_id", "address", "person", "date", "body"}, null, null, "DATE desc");


            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String smsBody = cur.getString(5);
                    String body=smsBody.toString().toLowerCase();
                    if (body.contains("happy") || body.contains("yay") || body.contains("I am well") || body.contains("excited")) {
                        happy++;
                        //Toast.makeText(getApplicationContext(), "found"+s, Toast.LENGTH_SHORT).show();
                    }
                    if (body.contains("sad") || body.contains("not well") || body.contains("crying")|| body.contains("ill") || body.contains("leave me alone") || body.contains("i hate people")){
                        sad++;

                    }
                    if (body.contains("alone") ||body.contains("lonely") || body.contains("heart broken") || body.contains("Extremely sad")){
                        lonely++;
                    }
                    if (body.contains("joyful") || body.contains("exited") || body.contains("")){
                        joyful++;

                    }

                }
            }
}
    }, 60*15, 60*15, SECONDS);

    return super.onStartCommand(intent, flags, startId);
}

2 个答案:

答案 0 :(得分:1)

创建日历对象并为where子句

设置条件
private static final int FIFTEEN_MINUTES=  15 * 60 * 1000;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

String[] projection = {"address", "body"};
String whereAddress = "address = ?";
String whereDate = "date BETWEEN " + cal.getTimeInMillis() +
                   " AND " + (cal.getTimeInMillis() + FIFTEEN_MINUTES);
String where = DatabaseUtils.concatenateWhere(whereAddress, whereDate);

然后使用像这样的查询

 cursor = getContentResolver().query(inboxUri,
                                        projection,
                                        where,
                                        new String[]{phoneNumber},
                                        "date DESC");

这会收到过去15分钟的最新消息。

答案 1 :(得分:0)

mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI,   // The content URI of the words table
mProjection,                        // The columns to return for each row
mSelectionClause                    // Selection criteria
mSelectionArgs,                     // Selection criteria
mSortOrder); 

您将选择条件设置为NULL; 将mSelectionClause替换为WHERE子句:WHERE col = value

"date>=" + dateStart.getTime();