3、 Uri:Uri是ContentResolver和ContentProvider进行数据交换的标识。
- 外界的程序通过ContentResolver接口可以访问ContentProvider提供的数据;
- ContentResolver 可以理解成是HttpClient的作用。
短信的相关操作
package com.hht.android15_contentresolversms;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView listView_main_sms;
private Cursor cursor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView_main_sms = (ListView) findViewById(R.id.listView_main_sms);
ContentResolver contentResolver = getContentResolver();
String uri = "content://sms";
cursor = contentResolver.query(Uri.parse(uri), new String[] { "_id",
"address", "date", "type", "body" }, null, null, null);
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
List
通话记录的相关操作package com.hht.android15_contentcontacts;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView listView_main_contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView_main_contacts = (ListView) findViewById(R.id.listView_main_contacts);
ContentResolver contentResolver = getContentResolver();
String uri = "content://call_log/calls";
Cursor cursor = contentResolver.query(Uri.parse(uri), new String[] {
"_id", "number", "type", "date" }, null, null, null);
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
List> list = helper.cursorToList(cursor);
MyAdapter adapter = new MyAdapter(this, list);
listView_main_contacts.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public String timeStampToDate(long timeStamp) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
return dateFormat.format(new Date(timeStamp));
}
class MyAdapter extends BaseAdapter {
private Context context;
private List> list = null;
public MyAdapter(Context context, List> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.item_list_main, null);
holder.textView_number = (TextView) convertView
.findViewById(R.id.textView_number);
holder.textView_type = (TextView) convertView
.findViewById(R.id.textView_type);
holder.textView_date = (TextView) convertView
.findViewById(R.id.textView_date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView_number.setText(list.get(position).get("number")
.toString());
holder.textView_type.setText(list.get(position).get("type")
.toString());
holder.textView_date.setText(timeStampToDate((Long) list.get(
position).get("date")));
if (list.get(position).get("type").toString().equals("2")) {
convertView.setBackgroundColor(Color.GRAY);
}
return convertView;
}
class ViewHolder {
private TextView textView_number;
private TextView textView_type;
private TextView textView_date;
}
}
}
package com.hht.android15_contentcontacts;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
public class MyContactsHelper {
// 获取联系人信息
public static List> selectContactsMsg(
ContentResolver resolver) {
List> list = new ArrayList>();
String uri_contacts = "content://com.android.contacts/raw_contacts";
String uri_contacts_phones = "content://com.android.contacts/data/phones";
String uri_contacts_emails = "content://com.android.contacts/data/emails";
// 从raw_contacts表中或许联系人的id和联系人的姓名。
Cursor cursor_contacts = resolver.query(Uri.parse(uri_contacts),
new String[] { "_id", "display_name" }, null, null, null);
// 遍历所有的联系人的信息
while (cursor_contacts.moveToNext()) {
int contacts_id = cursor_contacts.getInt(cursor_contacts
.getColumnIndex("_id"));
String display_name = cursor_contacts.getString(cursor_contacts
.getColumnIndex("display_name"));
Map map = new HashMap();
map.put("_id", contacts_id);
map.put("display_name", display_name);
// 以下开始获取电话号码
// 根据每个联系人的id再去data表中查找相应的电话号码。
Cursor cursor_phones = resolver.query(
Uri.parse(uri_contacts_phones), new String[] {
"raw_contact_id", "data1" }, "raw_contact_id=?",
new String[] { contacts_id + "" }, null);
// 因为电话号码可能是多个,所以需要再遍历,组合在一起形成一个电话号码的字符串,放到StringBuilder中
StringBuilder sb = new StringBuilder();
while (cursor_phones.moveToNext()) {
sb.append(cursor_phones.getString(1));
sb.append(" | ");
}
// 将生成的电话号码放到map集合中
map.put("phones", sb.toString());
// 以下开始或许Email信息
Cursor cursor_emails = resolver.query(
Uri.parse(uri_contacts_emails), new String[] {
"raw_contact_id", "data1" }, "raw_contact_id=?",
new String[] { contacts_id + "" }, null);
StringBuilder sb2 = new StringBuilder();
while (cursor_emails.moveToNext()) {
sb2.append(cursor_emails.getString(1));
sb2.append(" | ");
}
map.put("emails", sb2.toString());
// 将包含有id、联系人姓名、手机号码、emails的map放到list集合中
list.add(map);
}
return list;
}
// 以下代码是更新联系人姓名的方法。
public static boolean updateContactsName(ContentResolver resolver,
Map map, int id) {
String uri_contacts = "content://com.android.contacts/raw_contacts";
String uri_contacts_data = "content://com.android.contacts/data";
ContentValues values = new ContentValues();
// 修改raw_contacts表中的数据
values.put("display_name", map.get("display_name").toString());
values.put("display_name_alt", map.get("display_name").toString());
values.put("sort_key", map.get("display_name").toString());
values.put("sort_key_alt", map.get("display_name").toString());
int count1 = resolver.update(Uri.parse(uri_contacts), values,
"_id=?", new String[] { id + "" });
// 修改data表姓名的数据
values.clear();
values.put("data1", map.get("display_name").toString());
values.put("data2", map.get("display_name").toString());
int count2 = resolver.update(Uri.parse(uri_contacts_data), values,
"raw_contact_id=? and mimetype_id=?", new String[] { id + "", "7" });
// 修改data表中phone的数据
values.clear();
values.put("data1", map.get("phone").toString());
values.put("data2", 2);
int count3 = resolver.update(Uri.parse(uri_contacts_data), values,
"raw_contact_id =? and mimetype_id=?", new String[] { id + "", "5" });
// 修改data表中email的数据
values.clear();
values.put("data1", map.get("email").toString());
values.put("data2", 1);
int count4 = resolver.update(Uri.parse(uri_contacts_data), values,
"raw_contact_id=? and mimetype_id=?", new String[] { id + "", "1" });
if (count1 > 0 && count2 > 0 && count3 > 0 && count4 > 0) {
return true;
} else {
return false;
}
}
public static void insertContacts(ContentResolver resolver,
List list) {
String uri_rawcontacts = "content://com.android.contacts/raw_contacts";
String uri_contacts_data = "content://com.android.contacts/data";
Uri uri = Uri.parse(uri_contacts_data);
// 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId
ContentValues values = new ContentValues();
Uri rawContactUri = resolver.insert(Uri.parse(uri_rawcontacts), values);
long contact_id = ContentUris.parseId(rawContactUri);
// 向data表中添加用户名称信息
values.put("raw_contact_id", contact_id);
values.put("mimetype", "vnd.android.cursor.item/name");
values.put("data1", list.get(0));
values.put("data2", list.get(0));
resolver.insert(uri, values);
// 往data表中插入电话信息
values.clear();
values.put("raw_contact_id", contact_id);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data1", list.get(1));
values.put("data2", 2);// 2,Phone.TYPE_MOBILE ,表示手机号码
resolver.insert(uri, values);
// 往data表中插入Email信息
values.clear();
values.put("raw_contact_id", contact_id);
values.put("mimetype", "vnd.android.cursor.item/email_v2");
values.put("data1", list.get(2));
values.put("data2", 2);// 2,Email.TYPE_WORK , 表示工作用Email号码
resolver.insert(uri, values);
}
// 以下代码是删除联系人信息的方法。
public static int deleteContacts(ContentResolver resolver, String where,
String[] whereArgs) {
String uri_contacts = "content://com.android.contacts/raw_contacts";
return resolver.delete(Uri.parse(uri_contacts), where, whereArgs);
}
}
(一)、Android系统管理联系人的Uri如下:
(二)、Android为多媒体提供的ContentProvider的Uri如下:
【数据库中主要字段:】
(三)、短信Uri:
(四)、通话记录Uri: