Android ContentProvider查看读取手机联系人实例

Android ContentProvider查看读取手机联系人实例,第1张

概述看到某些App里面有读取联系人的功能,然后自己尝试了一下。发现这个挺简单的。然后自己就做了一个demo给大家,希望借这个demo可以让大家学习一下怎么实现读取手机联系人

看到某些App里面有读取联系人的功能,然后自己尝试了一下。发现这个挺简单的。然后自己就做了一个demo给大家,希望借这个demo可以让大家学习一下怎么实现读取手机联系人。

这里我用了两种方法去读取:第一张图片是跳转到系统自带的联系人界面,第二种就是直接去读取让后绑上来显示在主页面。话不多说直接上代码。

记得在AndroIDManifest.xml 记得加入这两句,不然就读取不到联系人。

<uses-permission androID:name="androID.permission.READ_CONTACTS" /><uses-permission androID:name="androID.permission.WRITE_CONTACTS" />
package com.example.androID_contentersolver;import androID.app.Activity;import androID.app.AlertDialog;import androID.content.DialogInterface;import androID.content.Intent;import androID.database.Cursor;import androID.net.Uri;import androID.os.Bundle;import androID.provIDer.Contacts;import androID.provIDer.ContactsContract;import androID.telephony.PhonestateListener;import androID.telephony.TelephonyManager;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.CheckBox;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class MainActivity extends Activity {  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    findVIEwByID(R.ID.main_btn).setonClickListener(new OnClickListener() {       @OverrIDe      public voID onClick(VIEw v) {        // Todo auto-generated method stub        //查询联系人号码        final Cursor cursor = getContentResolver()            .query(ContactsContract.CommonDataKinds            .Phone.CONTENT_URI,null,null);         BaseAdapter adapter = new BaseAdapter()         {           @OverrIDe           public int getCount()           {             return cursor.getCount();           }           @OverrIDe           public Object getItem(int position)           {             return position;           }           @OverrIDe           public long getItemID(int position)           {             return position;           }           @OverrIDe           public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent)             {               cursor.movetoposition(position);//               CheckBox rb = new CheckBox(MainActivity.this);               TextVIEw text=new TextVIEw(MainActivity.this);               // 获取联系人的电话号码,并去掉中间的中划线、空格               String number = cursor                 .getString(cursor.getColumnIndex(                 ContactsContract.CommonDataKinds                 .Phone.NUMBER))                 .replace("-","")                 .replace(" ","");//               rb.setText(number);               String name = cursor                   .getString(cursor.getColumnIndex(                   ContactsContract.CommonDataKinds                   .Phone.disPLAY_name));                text.setText("名字:"+name+"  电话号码:"+number);               return text;             }         };       //加载ListvIEw       ListVIEw ListvIEw=(ListVIEw) findVIEwByID(R.ID.mian_List);       ListvIEw.setAdapter(adapter);      }    });    //跳转系统的联系人界面    findVIEwByID(R.ID.main_btn_sys).setonClickListener(new OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        // Todo auto-generated method stub        Intent intent=new Intent();        intent.setAction(intent.ACTION_PICK);        intent.setType("vnd.androID.cursor.dir/phone");        startActivityForResult(intent,0x100);      }    });  }  @OverrIDe  protected voID onActivityResult(int requestCode,int resultCode,Intent data) {  // Todo auto-generated method stub  super.onActivityResult(requestCode,resultCode,data);    Uri uri=data.getData();    Cursor cursor = getContentResolver()        .query(uri,null);    while(cursor.movetoNext()){      String name=cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.name));      String number=cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.NUMBER));      TextVIEw textvIEw=(TextVIEw) findVIEwByID(R.ID.mian_Lists);      textvIEw.setText("名字:"+name+"  电话号码:"+number);    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android ContentProvider查看/读取手机联系人实例全部内容,希望文章能够帮你解决Android ContentProvider查看/读取手机联系人实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146993.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存