这个项目实现了从通讯录中获取联系人信息,也实现了将数据储存在数据库中,并对数据进行增删改查的 *** 作
项目结构如图
在进行获取通讯录信息之前要在AndroIDManifest.xml文件中添加如下代码:
<uses-permission androID:name="androID.permission.READ_CONTACTS"/>
主活动布局如下:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:orIEntation="vertical" androID:layout_height="match_parent" tools:context=".MainActivity"> <EditText androID:ID="@+ID/ed_name" androID:layout_wIDth="match_parent" androID:hint="姓名" androID:layout_margintop="25dp" androID:layout_height="wrap_content"/> <EditText androID:ID="@+ID/ed_phone" androID:layout_wIDth="match_parent" androID:hint="电话" androID:layout_margintop="25dp" androID:layout_height="wrap_content"/> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:layout_margintop="25dp" androID:orIEntation="horizontal"> <button androID:ID="@+ID/btn_add" androID:layout_marginRight="5dp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="添加"/> <button androID:ID="@+ID/btn_query" androID:layout_wIDth="wrap_content" androID:layout_marginRight="5dp" androID:layout_height="wrap_content" androID:text="查询"/> <button androID:ID="@+ID/btn_update" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginRight="5dp" androID:text="修改"/> <button androID:ID="@+ID/btn_delete" androID:layout_wIDth="wrap_content" androID:layout_marginRight="5dp" androID:layout_height="wrap_content" androID:text="删除"/> </linearLayout> <button androID:ID="@+ID/btn_Contact" androID:layout_wIDth="wrap_content" androID:layout_gravity="center" androID:layout_height="wrap_content" androID:text="获取通讯录联系人信息"/> <TextVIEw androID:ID="@+ID/tv_show" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/></linearLayout>
MainActivity.java:
package cn.itcast.tongxunludemo;import androIDx.annotation.NonNull;import androIDx.appcompat.app.AppCompatActivity;import androIDx.core.app.ActivityCompat;import androIDx.core.content.ContextCompat;import androID.Manifest;import androID.content.ContentResolver;import androID.content.ContentValues;import androID.content.Context;import androID.content.pm.PackageManager;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.net.Uri;import androID.os.Build;import androID.os.Bundle;import androID.provIDer.ContactsContract;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{private EditText edname;private EditText edPhone;private TextVIEw tvShow;private button btnAdd;private button btnquery;private button btnUpdate;private button btnDelete;private button btnContact;MyHelper myHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); edname=findVIEwByID(R.ID.ed_name); edPhone=findVIEwByID(R.ID.ed_phone); tvShow=findVIEwByID(R.ID.tv_show); btnAdd=findVIEwByID(R.ID.btn_add); btnquery=findVIEwByID(R.ID.btn_query); btnUpdate=findVIEwByID(R.ID.btn_update); btnDelete=findVIEwByID(R.ID.btn_delete); btnContact=findVIEwByID(R.ID.btn_Contact); myHelper=new MyHelper(this); btnAdd.setonClickListener((VIEw.OnClickListener) MainActivity.this); btnquery.setonClickListener((VIEw.OnClickListener) MainActivity.this); btnUpdate.setonClickListener((VIEw.OnClickListener) MainActivity.this); btnDelete.setonClickListener((VIEw.OnClickListener) MainActivity.this); btnContact.setonClickListener((VIEw.OnClickListener) MainActivity.this); } @OverrIDe public voID onClick(VIEw v) { String name,phone; sqliteDatabase db; ContentValues values; switch (v.getID()){ case R.ID.btn_add: name=edname.getText().toString(); phone=edPhone.getText().toString(); db=myHelper.getWritableDatabase(); String sqlSave="insert into information (name,phone) values(?,?)"; db.execsql(sqlSave,new String[]{name,phone}); Toast.makeText(this,"信息已经添加",Toast.LENGTH_SHORT).show(); db.close(); break; case R.ID.btn_query: db=myHelper.getReadableDatabase(); Cursor cursor=db.rawquery("select * from information",null); if(cursor.getCount()==0){ tvShow.setText(""); Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show(); }else { cursor.movetoFirst(); tvShow.setText("name:"+cursor.getString(1)+";Tel:"+cursor.getString(2)); } while (cursor.movetoNext()){ tvShow.append("\n"+"name:"+cursor.getString(1)+";Tel:"+cursor.getString(2)); } cursor.close(); db.close(); break; case R.ID.btn_update: String nername=edname.getText().toString(); String newPhoto=edPhone.getText().toString(); db=myHelper.getWritableDatabase(); String sqlUpdata="update information set phone=? where name=?"; db.execsql(sqlUpdata,new String[]{newPhoto,nername}); Toast.makeText(this,"信息已经修改",Toast.LENGTH_SHORT).show(); db.close(); break; case R.ID.btn_delete: db=myHelper.getWritableDatabase(); db.execsql("delete from information"); Toast.makeText(this,"信息已经删除",Toast.LENGTH_SHORT).show(); tvShow.setText(""); db.close(); break; case R.ID.btn_Contact: if(Build.VERSION.SDK_INT<23){ //获取联系人信息 getContacts(); }else { if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){ //发起动态权限申请 ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},1); }else { //获取联系人 getContacts(); } } break; } } @OverrIDe public voID onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode==1 && permissions[0].equals(Manifest.permission.READ_CONTACTS)&& grantResults[0]==PackageManager.PERMISSION_GRANTED){ //获取联系人信息 getContacts(); }else { Toast.makeText(MainActivity.this,"您拒绝了相关权限,无法获取联系人信息!",Toast.LENGTH_SHORT).show(); } } private voID getContacts(){ tvShow.setText(""); Uri uri= ContactsContract.CommonDataKinds.Phone.CONTENT_URI; ContentResolver resolver=getContentResolver(); Cursor cursor=resolver.query(uri,null,null,null,null); if(cursor !=null){ while (cursor.movetoNext()){ String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_name)); String phone=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); tvShow.append("\n姓名:"+name+".电话:"+phone); } } } class MyHelper extends sqliteOpenHelper{ public MyHelper(Context context){ super(context,"itcast.db",null,1); } @OverrIDe public voID onCreate(sqliteDatabase db) { db.execsql("create table information(_ID integer primary key autoincrement,name varchar(20),phone varchar(20))"); } @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { } }}
这样这个项目就完成了
总结
以上是内存溢出为你收集整理的使用Contact实现从通讯录中获取数据全部内容,希望文章能够帮你解决使用Contact实现从通讯录中获取数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)