contentProvider在电话联系人中应用

来源:互联网 发布:数控下料编程软件 编辑:程序博客网 时间:2024/05/09 09:24

1、手机中系统应用都提供了contentProvider方法,用于跨应用调用数据,电话联系人常用类ContactsContract.Contacts   主程序如下

package com.jackie.contactsproject;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.provider.ContactsContract;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MyDemo extends Activity {private ListView contactsList = null;private Cursor result = null;private List<Map<String, Object>> contacts = null;private SimpleAdapter simple = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.contactsList = (ListView) super.findViewById(R.id.contactsList);this.result = super.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);MyDemo.this.startManagingCursor(this.result);contacts = new ArrayList<Map<String, Object>>();for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result.moveToNext()) {Map<String, Object> contact = new HashMap<String, Object>();contact.put("_id", this.result.getInt(this.result.getColumnIndex(ContactsContract.Contacts._ID)));contact.put("name", this.result.getString(this.result.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));contacts.add(contact);}simple = new SimpleAdapter(MyDemo.this, this.contacts,R.layout.contacts, new String[] { "_id", "name" }, new int[] {R.id._id, R.id.name });this.contactsList.setAdapter(this.simple);super.registerForContextMenu(this.contactsList);}@Overridepublic boolean onContextItemSelected(MenuItem item) {AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();int position = info.position; // 取得操作的位置String contactsId = this.contacts.get(position).get("_id").toString();switch (item.getItemId()) { // 经行菜单的操作case Menu.FIRST + 1: // 查看详情String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "=?";String[] phoneSelectionArgs = new String[] { contactsId };Cursor c = super.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,            //管理联系人的手机号码的uriphoneSelection, phoneSelectionArgs, null);StringBuffer buf = new StringBuffer();buf.append("电话号码是:");for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {buf.append(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))).append("、");}Toast.makeText(this, buf, Toast.LENGTH_SHORT).show();break;case Menu.FIRST + 2: // 删除super.getContentResolver().delete(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,     //管理联系人的uri   与手机号码的uri有区别contactsId), null, null);this.contacts.remove(position);  //删除集合数据项this.simple.notifyDataSetChanged(); //通知改变Toast.makeText(this, "数据已删除!", Toast.LENGTH_SHORT).show();break;}return super.onContextItemSelected(item);}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { // 创建上下文菜单super.onCreateContextMenu(menu, v, menuInfo);menu.setHeaderTitle("联系人操作");menu.add(menu.NONE, menu.FIRST + 1, 1, "查看详情");menu.add(menu.NONE, menu.FIRST + 2, 2, "删除信息");}}

布局文件如下main.xml和contacts.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MyDemo" >    <TextView        android:id="@+id/mainInfo"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="60px"        android:text="联系人列表" />    <ListView         android:id="@+id/contactsList"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MyDemo" >    <TableRow >    <TextView        android:id="@+id/_id"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:padding="20dp"        android:textSize="40px"            />    <TextView        android:id="@+id/name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:padding="20dp"        android:textSize="40px"/>    </TableRow></TableLayout>

2、利用contentProvider取得通话记录 主程序如下
package com.jackie.callsproject;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.provider.CallLog;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleAdapter;public class MyDemo extends Activity {private ListView callList = null;private Cursor result = null;private SimpleAdapter simple = null;private List<Map<String, Object>> calls = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.callList = (ListView) super.findViewById(R.id.callList);this.result = super.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);System.out.println("******"+this.result.getCount());this.startManagingCursor(this.result);this.calls = new ArrayList<Map<String, Object>>();for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result.moveToNext()) {Map<String, Object> call = new HashMap<String, Object>();call.put("_id", this.result.getInt(this.result.getColumnIndex(CallLog.Calls._ID)));String tempName = this.result.getString(this.result.getColumnIndex(CallLog.Calls.CACHED_NAME));if (tempName == null || "".equals(tempName)) {tempName = "未知";}call.put("name", tempName);call.put("number", this.result.getString(this.result.getColumnIndex(CallLog.Calls.NUMBER)));calls.add(call);}this.simple = new SimpleAdapter(this, this.calls, R.layout.calls,new String[] { "_id", "name", "number" }, new int[] { R.id._id,R.id.name, R.id.number });this.callList.setAdapter(this.simple);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.my_demo, menu);return true;}}
Cursor本来就是集合,再进行集合取值、赋值,设置适配器很麻烦,现提供了SimpleCursorAdapter类可以对Cursor进行封装,并直接用于显示

package com.jackie.callsproject;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.provider.CallLog;import android.view.Menu;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.SimpleCursorAdapter;public class MyDemo extends Activity {private ListView callList = null;private Cursor result = null;    private ListAdapter listAdapter=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.callList = (ListView) super.findViewById(R.id.callList);this.result = super.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);System.out.println("******"+this.result.getCount());this.startManagingCursor(this.result);String[] calls=new String[]{CallLog.Calls._ID,CallLog.Calls.CACHED_NAME,CallLog.Calls.NUMBER};int[] entries=new int[]{R.id._id,R.id.name,R.id.number};this.listAdapter=new SimpleCursorAdapter(this,R.layout.calls,this.result,calls,entries);this.callList.setAdapter(this.listAdapter);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.my_demo, menu);return true;}}

ContentProvider可以将一个应用程序的数据操作交给外部处理


0 0