android:获取手机号码和姓名实现通讯录

网友投稿 1295 2022-10-22

android:获取手机号码和姓名实现通讯录

android:获取手机号码和姓名实现通讯录

首先是运行结果

由于通讯录在手机里是以数据库贮存的 所以我们可以通过一个方法

context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);

来获得通讯录 ,这个方法返回一个游标的数据类型,通过moveToNext()方法来获取所有的手机号码信息

布局文件

activity_main.xml

listview的布局文件:item.xml,在这里我设置的头像为默认的 当然也可以在手机数据库中读取联系人的icon

自己封装一个联系人信息的类 有两个变量

package com.example.getphonenumber;public class PhoneInfo { private String name; private String number; public PhoneInfo(String name, String number) { this.name = name; this.number = number; } public String getName() { return name; } public String getNumber() { return number; }}

读取手机数据库中的通讯录

GetPhoneNumberFromMobile.class

package com.example.getphonenumber;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.provider.ContactsContract.CommonDataKinds.Phone;public class GetPhoneNumberFromMobile { private List list; public List getPhoneNumberFromMobile(Context context) { // TODO Auto-generated constructor stub list = new ArrayList(); Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null); //moveToNext方法返回的是一个boolean类型的数据 while (cursor.moveToNext()) { //读取通讯录的姓名 String name = cursor.getString(cursor .getColumnIndex(Phone.DISPLAY_NAME)); //读取通讯录的号码 String number = cursor.getString(cursor .getColumnIndex(Phone.NUMBER)); PhoneInfo phoneInfo = new PhoneInfo(name, number); list.add(phoneInfo); } return list; }}

自定义adapter

package com.example.getphonenumber;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MyAdapter extends BaseAdapter{ private List list; private Context context; public MyAdapter(List list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ ViewHolder viewHolder=new ViewHolder(); LayoutInflater inflater=LayoutInflater.from(context); convertView=inflater.inflate(R.layout.item, null); viewHolder.name=(TextView) convertView.findViewById(R.id.name); viewHolder.number=(TextView) convertView.findViewById(R.id.number); viewHolder.name.setText(list.get(position).getName()); viewHolder.number.setText(list.get(position).getNumber()); } return convertView; } public class ViewHolder{ TextView name; TextView number; }}

package com.example.getphonenumber;import java.util.ArrayList;import java.util.List;import android-.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends Activity implements OnItemClickListener { private ListView lv; private MyAdapter adapter; private GetPhoneNumberFromMobile getPhoneNumberFromMobile; private List list = new ArrayList(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView1); getPhoneNumberFromMobile = new GetPhoneNumberFromMobile(); list = getPhoneNumberFromMobile.getPhoneNumberFromMobile(this); adapter = new MyAdapter(list, this); lv.setAdapter(adapter); lv.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub String number = list.get(position).getNumber(); Intent intent = new Intent(); intent.setAction("android.intent.action.CALL"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:FPSheet:使用Haskell作为脚本语言的Spreadsheet程序
下一篇:Laravel 5.7的无密码身份验证驱动程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~