How can I read SMS messages from the device programmatically in Android?

后端 未结 11 2508
情深已故
情深已故 2020-11-22 02:48

I want to retrieve the SMS messages from the device and display them?

11条回答
  •  情话喂你
    2020-11-22 03:06

    It is a trivial process. You can see a good example in the source code SMSPopup

    Examine the following methods:

    SmsMmsMessage getSmsDetails(Context context, long ignoreThreadId, boolean unreadOnly)
    long findMessageId(Context context, long threadId, long _timestamp, int messageType
    void setMessageRead(Context context, long messageId, int messageType)
    void deleteMessage(Context context, long messageId, long threadId, int messageType)
    

    this is the method for reading:

    SmsMmsMessage getSmsDetails(Context context,
                                long ignoreThreadId, boolean unreadOnly)
    {
       String SMS_READ_COLUMN = "read";
       String WHERE_CONDITION = unreadOnly ? SMS_READ_COLUMN + " = 0" : null;
       String SORT_ORDER = "date DESC";
       int count = 0;
       // Log.v(WHERE_CONDITION);
       if (ignoreThreadId > 0) {
          // Log.v("Ignoring sms threadId = " + ignoreThreadId);
          WHERE_CONDITION += " AND thread_id != " + ignoreThreadId;
       }
       Cursor cursor = context.getContentResolver().query(
                          SMS_INBOX_CONTENT_URI,
                          new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                          WHERE_CONDITION,
                          null,
                          SORT_ORDER);
       if (cursor != null) {
          try {
             count = cursor.getCount();
             if (count > 0) {
                cursor.moveToFirst();
                // String[] columns = cursor.getColumnNames();
                // for (int i=0; i

提交回复
热议问题