现在就实现具体的功能-----添加黑名单
先看图:
从图中也可以看出整个逻辑,就是:
1. 点击底部的添加按钮
2. 转到联系人选择界面选择联系人(这里调用的是系统的联系人界面,每次只能选择一个联系人,当然了,要实现每次选择多个联系人也可以,可以自定义选择联系人界面)
3. 返回选择的联系人并插入到数据库中。
4. 遍历数据库中的黑名单表,将所有的黑名单显示在ListView中
5. 删除联系人
6. 刷新黑名单显示列表
主要代码(BlackListActivity.java):
private Button btn_add;
btn_add = (Button)findViewById(R.id.btn_add); btn_add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); } });
这里的Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI是跳转到系统的选择联系人界面,因为要返回选择的联系人信息,所以要startActivityForResult,其中1,指是requestCode即请求代码,自然也要实现onActivityResult方法了,如下所示:
protected void onActivityResult (int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK){ if(requestCode == 1){ System.out.println("-------------------"); Uri uri = data.getData(); create_db(); Cursor cursor = managedQuery(uri, null, null, null, null); cursor.moveToFirst(); String result[] = this.getContactPhone(cursor); insert_db(result[0], result[1]);//这里是用的是数据库,当然,可以用List来保存联系人的姓名与电话 showBlackList(); db.close(); } } } private String[] getContactPhone(Cursor cursor){ int phoneColum = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); int phoneNum = cursor.getInt(phoneColum); String result[]= new String[2]; if(phoneNum > 0){ //取得联系人的ID号 int idColum = cursor.getColumnIndex(ContactsContract.Contacts._ID); String contactId = cursor.getString(idColum); //取出联系人电话 Cursor phone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null); if(phone.moveToFirst()){ for(;!phone.isAfterLast();phone.moveToNext()){ int index = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int typeindex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); int name = phone.getColumnIndex(PhoneLookup.DISPLAY_NAME); String phoneNumber = phone.getString(index); String phoneName = phone.getString(name); result[0] = phoneName; result[1] = phoneNumber; } } } return result; }
通过getContactPhone(Cursor cursor)方法 就可以返回选择的联系人的姓名与号码,这里将返回的联系人信息保存在数据库中,并遍历数据库中的表,将其显示在界面中。
既然用到了数据库,当然少不了数据库的创建与关闭,以及数据库的增删改查,下面为数据库相关代码
private List<Person> array = new ArrayList<Person>(); private SQLiteDatabase db;
//创建或打开数据库 public void create_db(){ //创建或打开数据库 db = SQLiteDatabase.openOrCreateDatabase(BlackListActivity.this.getFilesDir().toString()+"/list.db3", null); //db.execSQL("DROP TABLE IF EXISTS blackList"); if(db == null){ Toast.makeText(BlackListActivity.this,"数据库创建不成功",Toast.LENGTH_LONG).show(); } else{ //Toast.makeText(MainActivity.this,"数据库创建成功",Toast.LENGTH_LONG).show(); /* //创建表 db.execSQL("create table if not exists blackList(_id integer primary key autoincrement," + "name varchar(50)," + "number varchar(15));");*/ } }
//插入 public void insert_db(String name,String number){ number = number.replaceAll("-",""); Cursor cursor = db.rawQuery("select * from blackList where number = "+number,null); if(cursor.getCount() == 0){ db.execSQL("insert into blackList(name,number) values('"+ name+ "','" + number +"');"); } else{ Toast.makeText(BlackListActivity.this, "该号码已在黑名单中", Toast.LENGTH_SHORT).show(); } cursor.close(); }<br abp="823" /><br abp="824" />
//删除 public void delete_db(int item_ID){ db.execSQL("delete from blackList where _id='" + item_ID+"'"); }
//显示黑名单 public void showBlackList(){ Cursor cursor = db.rawQuery("select * from blackList",null); array.clear(); if(cursor.getCount() > 0){ cursor.moveToFirst(); for(int i = 0;i < cursor.getCount();i++){ int idIndex = cursor.getColumnIndex("_id"); int nameIndex = cursor.getColumnIndex("name"); int numberIndex = cursor.getColumnIndex("number"); int id = cursor.getInt(idIndex); String name = cursor.getString(nameIndex); String number = cursor.getString(numberIndex); Person person = new Person(id,name,number); array.add(person); cursor.moveToNext(); } } cursor.close(); adapter = new Adapter(BlackListActivity.this,tv_count); lv_show.setAdapter(adapter); }
添加联系人并显示的功能,已经实现了,那么剩下的就是删除联系人了,当长按listView中的item时,会弹出多选控件以及底部的删除与返回按钮控件,关于如何弹出这些控件,在上一节专门写了,这里只是在上节的基础上实现删除联系人。
private List<Person> selectid = new ArrayList<Person>(); private boolean isMulChoice = false; private Adapter adapter; private ListView lv_show; private RelativeLayout add_layout; private RelativeLayout delete_layout; private Button btn_delete; private Button btn_cancel;
btn_delete = (Button)findViewById(R.id.btn_delete); btn_delete.setOnClickListener(new ClickEvent()); btn_cancel = (Button)findViewById(R.id.btn_cancel); btn_cancel.setOnClickListener(new ClickEvent());
class ClickEvent implements OnClickListener{ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_cancel: isMulChoice = false; selectid.clear(); adapter = new Adapter(BlackListActivity.this,tv_count); lv_show.setAdapter(adapter); delete_layout.setVisibility(View.GONE); add_layout.setVisibility(View.VISIBLE); break; case R.id.btn_delete: isMulChoice =false; create_db(); for(int i=0;i<selectid.size();i++){ for(int j=0;j<array.size();j++){ if(selectid.get(i).equals(array.get(j))){ delete_db(array.get(j).id); array.remove(j); } } } selectid.clear(); showBlackList(); db.close(); adapter = new Adapter(BlackListActivity.this,tv_count); lv_show.setAdapter(adapter); delete_layout.setVisibility(View.GONE); add_layout.setVisibility(View.VISIBLE); break; default: break; } } }
删除的思路就是当点击删除按钮时,将选中的联系从数据库中删除,并调用showBlackList来刷新显示黑名单列表。
最后,用到了选择联系人,那么就要有读取联系人信息的权限,在AndroidManifest.xml添加:
<uses-permission android:name="android.permission.READ_CONTACTS"/> <br abp="855" />