How to read the message content of a new in coming message in android?

前端 未结 5 1206
深忆病人
深忆病人 2020-12-04 20:42

I want to read the message body of a new incoming SMS in android, programmatically.

I tried something but that doesn\'t return any contents:

5条回答
  •  醉话见心
    2020-12-04 20:55

    listitem=(ListView)findViewById(R.id.list_view);
    
                Uri mSmsQueryUri = Uri.parse("content://sms/inbox");
                List messages = new ArrayList();
    
                Cursor cursor = null;
                try {
                    cursor = getContentResolver().query(mSmsQueryUri, null, null, null, null);
                    if (cursor == null) {
                       // Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
    
                    }
                    for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) {
                        final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
                        final String sender_no= cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
                        final String date= cursor.getString(cursor.getColumnIndexOrThrow("date"));
                        final String type =cursor.getString(cursor.getColumnIndexOrThrow("type"));
    
    
                        messages.add(body);
                        messages.add(sender_no);
                        messages.add(date);
                        messages.add(type);
                    }
                } catch (Exception e) {
                    //Log.e(TAG, e.getMessage());
                } finally {
                    cursor.close();
                }
    
                listitem.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,messages));
         }
    }
    

提交回复
热议问题