代码:
package com.qianfeng.contentprovidercalllog;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ToggleButton;
/**
*
* 获取打电话的记录
*/
public class MainActivity extends Activity {
private ListView lv;
private ContentResolver resolver;// 内容解析者
// private String path="content://call_log/calls";
// Uri uri = Uri.parse(path);
private Uri callLog = CallLog.Calls.CONTENT_URI;// 访问通信记录的Uri对象
private List<Map<String, Object>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<Map<String,Object>>();
lv = (ListView) findViewById(R.id.lv);
resolver = getContentResolver();// 得到内容解析者的对象
Cursor cursor = resolver.query(callLog, null, null, null, null);
list = getData(cursor); // 获取数据源
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.item, new String[] { "number", "date" }, new int[] {
R.id.textView1, R.id.textView2 });
lv.setAdapter(adapter);
registerForContextMenu(lv);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo MenuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
int position = MenuInfo.position;
int id = (Integer) list.get(position).get("id");//根据下标位置 得到id
switch (item.getItemId()) {
case R.id.add_data:// 添加
InsertData();
break;
case R.id.update_data:// 修改
UpdateData(id);
break;
case R.id.delete_data:// 删除
DeleteData(id);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
/**
* 修改点击的item的数据
* @param id
*/
public void UpdateData(int id) {
ContentValues values = new ContentValues();
values.put("number", "110");
int flag = resolver.update(callLog, values,
CallLog.Calls._ID+"=?", new String []{id+""});
if(flag>0){
Toast.makeText(MainActivity.this, "修改成功", 0).show();
}else{
Toast.makeText(MainActivity.this, "修改不成功。。。", 0).show();
}
}
/**
* 删除点击的item数据
* @param id
*/
public void DeleteData(int id){
int flag = resolver.delete(callLog, CallLog.Calls._ID+"=?", new String []{id+""});
if(flag>0){
Toast.makeText(MainActivity.this, "删除成功", 0).show();
}else{
Toast.makeText(MainActivity.this, "删除不成功", 0).show();
}
}
/**
* 向里插入数据
*/
public void InsertData() {
/**
* 参数1:Uri资源表示 参数2:ContentValues 对象
*/
ContentValues values = new ContentValues();
values.put(CallLog.Calls._ID, 47);
values.put(CallLog.Calls.NUMBER, "12345678911");
values.put(CallLog.Calls.DATE, System.currentTimeMillis());
values.put(CallLog.Calls.TYPE, 1);
resolver.insert(callLog, values);
}
/**
* 获取cursor里面的数据
*
* @return
*/
public List<Map<String, Object>> getData(Cursor cursor) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
while (cursor.moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
// 得到电话号码和日期
String number = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
int date = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DATE));
int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
String strDate = GetDate(date);
map.put("number", number);
map.put("date", strDate);
map.put("id", id);
list.add(map);
}
return list;
}
/**
* int 类型时间 转换成string
*
* @param date
* @return
*/
public String GetDate(int date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(new Date(date));
}
}
请单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qianfeng.contentprovidercalllog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 加读取电话记录权限 -->
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qianfeng.contentprovidercalllog.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>