检索联系人号码和消息名称和日期

时间:2012-03-30 09:21:09

标签: android android-contacts

我想显示联系人列表,因此我开发了这段代码:

private void ListContact() {
    // instance qui permet d'accéder au contenu d'autre application
    ContentResolver ConnectApp = this.getContentResolver();
    Uri uri = Contacts.People.CONTENT_URI;
    String[] projection = new String[] {People.NAME, People.NUMBER, People._ID };
    // on récupère les contacts dans un curseur
    Cursor cur = ConnectApp.query(uri, projection, null, null, null);
    //this.startManagingCursor(cur);

    if (cur.moveToFirst()) {
        do {
            String name = cur.getString(cur.getColumnIndex(People.NAME));
            String num = cur.getString(cur.getColumnIndex(People.NUMBER));
            String id = cur.getString(cur.getColumnIndex(Contacts.People._ID));
            Log.i("contacts", " name: "+name+" num: "+num+" id: "+id);
        } while (cur.moveToNext());
    }
}

但是在logcat中它返回num:NULL 对于我开发此代码的消息:

public void read () {
    Uri mSmsinboxQueryUri = Uri.parse("content://sms");
    Cursor cursor1 = getContentResolver().query(
            mSmsinboxQueryUri,
            new String[] { "_id", "thread_id", "address", "person", "date",
                    "body", "type" }, null, null, null);
    //context.getContentResolver().startManagingCursor(cursor1);
    String[] columns = new String[] { "address", "person", "date", "body",
            "type" };
    if (cursor1.getCount() > 0) {
        String count = Integer.toString(cursor1.getCount());
        Log.e("Count",count);
        while (cursor1.moveToNext()) {      

            String address = cursor1.getString(cursor1
                    .getColumnIndex(columns[0]));
            String name = cursor1.getString(cursor1
                    .getColumnIndex(columns[1]));
            String date = cursor1.getString(cursor1
                    .getColumnIndex(columns[2]));
            String msg = cursor1.getString(cursor1
                    .getColumnIndex(columns[3]));
            String type = cursor1.getString(cursor1
                    .getColumnIndex(columns[4]));
           // if (type=="1"){type="received";} else {type="sent";}
            Log.i("msg","msg: "+ msg + " address: "+address+ " name: "+name+ " date:   "+date+ " type: "+type);            
        }
    }
}

我在这种情况下名称:NULL并且日期格式错误

0 个答案:

没有答案