在Android 2.2上读取联系人
我使用以下代码在 Android 2.2 上读取电话联系人姓名和号码。
我想要做的是读取每个联系人姓名和号码并将其附加到 Stringbuilder
。
目前我可以获取联系人姓名,但获取联系电话时遇到一些问题。
我怎样才能实现这个目标?请提供示例片段以获取数字。
public class main extends Activity {
/** Called when the activity is first created. */
public static StringBuilder s = new StringBuilder();
public static String number= new String();// = new String();
public static String contact = new String();
public final String phonenumber = new String();
public String formattedPhoneNumber = new String();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//-----------code to run when button is clicked-------------------
EditText textfield = (EditText) findViewById(R.id.textfield);
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, null,null, null);
if(cursor != null) {
if(cursor.getCount() > 0) {
cursor.moveToFirst();
formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
//Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
}
cursor.close();
while(people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
contact = people.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
//umber = people.getString(numberFieldColumnIndex);
}
people.close();
//--------------code for getting phone number---
textfield.setText("hello: "+contact+" "+ formattedPhoneNumber);
//+String.valueOf(number));
//---------------end of code-----------------------------
}
};});}}
I'm using the following code to read phone contact names and numbers on Android 2.2.
What i want to do is go reading each contact name and number and append that to a Stringbuilder
.
Presently I'm able to get the contact name, but there's some trouble getting the contact numbers.
How can I achieve this? Please provide a sample snippet to get the numbers.
public class main extends Activity {
/** Called when the activity is first created. */
public static StringBuilder s = new StringBuilder();
public static String number= new String();// = new String();
public static String contact = new String();
public final String phonenumber = new String();
public String formattedPhoneNumber = new String();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//-----------code to run when button is clicked-------------------
EditText textfield = (EditText) findViewById(R.id.textfield);
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, null,null, null);
if(cursor != null) {
if(cursor.getCount() > 0) {
cursor.moveToFirst();
formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
//Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
}
cursor.close();
while(people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
contact = people.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
//umber = people.getString(numberFieldColumnIndex);
}
people.close();
//--------------code for getting phone number---
textfield.setText("hello: "+contact+" "+ formattedPhoneNumber);
//+String.valueOf(number));
//---------------end of code-----------------------------
}
};});}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
复制自此处:
Copied from here: