这个项目实现了从通讯录中获取联系人信息,也实现了将数据储存在数据库中,并对数据进行增删改查的操作
项目结构如图
在进行获取通讯录信息之前要在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) { } } }
这样这个项目就完成了