1

这是我的代码:

private void readSMS() throws IOException {
    // TODO Auto-generated method stub
    Log.d("Read SMS","Called");
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.parse("content://sms/inbox");
    String smsBackup; 
    Cursor messagesCursor = cr.query(uri, new String[] { "_id","address","body","person"}, null,null, null);

    smsBackup = "SMS Back UP (Total Message(s)::"+messagesCursor.getCount()+") \n\n";

    String smsfile = "SMS" + "_" + System.currentTimeMillis()+".txt";

    storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + smsfile;
    FileOutputStream mFileOutputStream = new FileOutputStream(storage_path,true);

    mFileOutputStream.write(smsBackup.getBytes());

    String name = null,smsString;

    int smsCounter = 1;
    if(messagesCursor.getCount() > 0){
        while(messagesCursor.moveToNext()){
            name = null;
            name = getName(messagesCursor.getString(messagesCursor.getColumnIndex("address")));

            if(name==null)
                name = "Sender : " + messagesCursor.getString(messagesCursor.getColumnIndex("address"));

            smsString = "SMS No : "+smsCounter+"\nSender : "+name +"\n"+  "Message : "+messagesCursor.getString(messagesCursor.getColumnIndex("body")) + "\n\n";

            mFileOutputStream.write(smsString.getBytes());

            Log.d("Message","::"+smsString+"Length::"+smsString.length());

            smsString = null;

            Log.d("Message","written::"+smsCounter);
            smsCounter++;
        }
        messagesCursor.close();
        file = new File(storage_path);
        mFileOutputStream.close();
        Log.d("MSGFile","written");
    }
}

private String getName(String number1) {
    // TODO Auto-generated method stub
    Log.d("get name","Called");
    //Log.d("get name",number1);

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

    if(cur.getCount() > 0){
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };
        Cursor names = getContentResolver().query(uri, projection, null, null, null);
        int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        names.moveToFirst();
        do {
            String name   = names.getString(indexName);
            //Log.e("Name new:", name);
            String number = names.getString(indexNumber);
            //Log.e("Number new:","::"+number);
            if(number1.contains(number)){
                cur.close();
                return name;
            }
        } while (names.moveToNext());
    }
    cur.close();
    return null;
}

我也收到警告:W/CursorWrapperInner(22787): Cursor finalized without prior close()

4

1 回答 1

3

使用下面的代码获取联系人

            private void readMessagesFromDeviceDB()
                {
                    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")); //returns a unique thread id
                                String address  = cursor.getString(cursor.getColumnIndex("address")); //returns contact no.
                                String body     = cursor.getString(cursor.getColumnIndex("body")); //returns message body
                                String date     = cursor.getString(cursor.getColumnIndex("date")); //returns date( when was the message received )

                                SimpleDateFormat formatter  = new SimpleDateFormat("dd, MMM HH:mm");
                                date = formatter.format(new Date(Long.parseLong(date)));


            //                System.out.println("id: " + id + "  address: " + address + "  body: " + body + "  date: " + date);
                            } 
                            while (cursor.moveToNext());
                        }
                    } finally {
                        if (cursor != null) 
                        {
                            cursor.close();
                        }
                    }
                }
于 2013-03-08T11:10:38.923 回答