【问题标题】:Not getting the phone number in android没有在android中获取电话号码
【发布时间】:2010-12-17 12:24:47
【问题描述】:

我尝试了以下代码,它会显示“姓名”,但不显示“号码”。
有人可以指出我在哪里犯了错误吗?

package org.testcont;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.widget.EditText;
import android.widget.TextView;

public class testcontActivity extends Activity {
    /** Called when the activity is first created. */
    TextView tvname;
    TextView tvnumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvname = (TextView) findViewById(R.id.TextView01);
        tvnumber = (TextView) findViewById(R.id.TextView02);

        Cursor c=getContentResolver().query(People.CONTENT_URI,null,null,null,null);
        while(c.moveToNext()){
        int name=c.getColumnIndexOrThrow(People.NAME);
        String nm=c.getString(name);
        tvname.setText(nm);
        int number=c.getColumnIndexOrThrow(People.NUMBER);
        String s=c.getString(number);

        }
        c.close();
    }
}

【问题讨论】:

    标签: android android-contacts phone-number


    【解决方案1】:

    您使用的是较旧的 API,而应该使用这个

    android.provider.ContactsContract.CommonDataKinds.Phone
    

    检索联系人姓名和号码的类也进行了以下更改:

    Cursor c=getContentResolver().query(Phone.CONTENT_URI,null,null,null,null);
    int name=c.getColumnIndexOrThrow(Phone.DISPLAY_NAME);
    int number=c.getColumnIndexOrThrow(Phone.NUMBER);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      相关资源
      最近更新 更多