Retrieve all inbox messages of device in android

一曲冷凌霜 提交于 2019-12-11 16:11:28

问题


I am using this code.

Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); 

SmsMessage[] messages = new SmsMessage[pduArray.length]; 

StringBuilder messageText = new StringBuilder();

for (int i = 0; i < pduArray.length; i++) {                 
     messages[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);              
     messageText.append(messages[i].getMessageBody());          
} 

But this code is for only intimate when sms is received in a device but my need is to retrieve all inbox messages which is saved in android device in android how can I do it?


回答1:


Try this code :

   Uri SMSURI = Uri.parse("content://sms/inbox");
   String[] projection = new String[]{"_id", "address", "body", "date"};
   Cursor cursor = null;

   try {
        cursor = getContentResolver().query(SMSURI
                , projection
                , null //selection
                , null //selectionArgs
                , null); //sortOrder

        if (cursor != null && cursor.moveToFirst()) {
            do {
                int    id    = cursor.getInt(cursor.getColumnIndex("_id"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String body = cursor.getString(cursor.getColumnIndex("body"));
                String date = cursor.getString(cursor.getColumnIndex("date"));

                Toast.makeText(getBaseContext(), "Message is >>"+body, 
                            Toast.LENGTH_SHORT).show();
                }

            } while (cursor.moveToNext());
        }

    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }


来源:https://stackoverflow.com/questions/8209507/retrieve-all-inbox-messages-of-device-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!