通讯录的访问-Call Log 访问
import java.sql.Date; import java.text.SimpleDateFormat; import java.util.Random; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.os.Bundle; import android.provider.CallLog; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class UltraCallLogActivity extends Activity { private ListView mCallLogList; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mCallLogList = (ListView) findViewById(R.id.listView); mCallLogList.setOnCreateContextMenuListener(this); OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub String string = new String("You clicked the listView!"); string = string + "\nPosittion: " + arg2; string = string + "\nId: " + arg3; string = string + "\nObject: " + mCallLogList.getItemAtPosition(arg2).toString(); DisplayToast(string); } }; mCallLogList.setOnItemClickListener(listener); InsertCallLog(); // ClearCallLog(); // DisplayCallLog(); DeleteCallLogByNumber(new String[] { "44444" }); CallLogListSetAdapte(); } public void DisplayCallLog() { String[] projection = new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE, CallLog.Calls.DATE }; Cursor c = GetCallLog(projection); DisplayCursor(c); c.close(); } public void CallLogListSetAdapte() { String[] column = new String[] { CallLog.Calls.NUMBER // , }; int[] views = new int[] { android.R.id.text1 // , android.R.id.text1 }; Cursor c = GetCallLog(null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, column, views); mCallLogList.setAdapter(adapter); } public Cursor GetCallLog(String[] projection) { ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls.DEFAULT_SORT_ORDER); return cursor; } // 删除某联系人最近的一次来电 public void DeleteCallLogByNumber(String[] number) { ContentResolver resolver = getContentResolver(); // 这里涉及到内容提供者的知识,其实这里是直接在操作 Android 的数据库 Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[] { "_id" }, "number=?", number, "_id desc limit 1"); if (cursor.moveToFirst()) { int id = cursor.getInt(0); resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[] { id + "" }); } } // Clear CallLog public void ClearCallLog() { ContentResolver resolver = getContentResolver(); resolver.delete(CallLog.Calls.CONTENT_URI, null, null); } public void InsertCallLog() { Cursor cursor = GetCallLog(null); if (cursor.getCount() > 30) { return; } ContentValues values = new ContentValues(); for (int i = 0; i < 10; i++) { Random random = new Random(); values.put(CallLog.Calls.NUMBER, "13621354" + random.nextInt(1000)); // values.put(CallLog.Calls.CACHED_NAME, "Jack" + i); values.put(CallLog.Calls.DATE, System.currentTimeMillis()); values.put(CallLog.Calls.TYPE, CallLog.Calls.MISSED_TYPE);// 未接 values.put(CallLog.Calls.NEW, "1");// 0已看1未看 InsertCallLogByContentValues(values); } } public void InsertCallLogByContentValues(ContentValues values) { ContentResolver cr = getContentResolver(); cr.insert(CallLog.CONTENT_URI, values); } public void DisplayCursor(Cursor cursor) { while (cursor.moveToNext()) { String str = "Number:\t\t\t" + cursor.getString(0); str = str + "\nName:\t\t\t" + cursor.getString(1); str = str + "\nType:\t\t\t" + cursor.getInt(2); SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(Long.parseLong(cursor.getString(3))); str = str + "\nDate:\t\t\t" + sfd.format(date); DisplayToast(str); } } public void DisplayToast(String s) { Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } // /////////////////////////Menu Process//////////////////////////////// @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); CreateMenu(menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); CreateMenu(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return MenuChoice(item); } @Override public boolean onContextItemSelected(MenuItem item) { return MenuChoice(item); } private void CreateMenu(Menu menu) { MenuItem mnu1 = menu.add(0, 0, 0, "Clear All"); { mnu1.setAlphabeticShortcut('a'); mnu1.setIcon(R.drawable.ic_launcher); } MenuItem mnu2 = menu.add(0, 1, 1, "Edit"); { mnu2.setAlphabeticShortcut('b'); mnu2.setIcon(R.drawable.ic_launcher); } MenuItem mnu3 = menu.add(0, 2, 2, "Delete"); { mnu3.setAlphabeticShortcut('c'); mnu3.setIcon(R.drawable.ic_launcher); } MenuItem mnu4 = menu.add(0, 3, 3, "Insert"); { mnu4.setAlphabeticShortcut('d'); mnu4.setIcon(R.drawable.ic_launcher); } MenuItem mnu5 = menu.add(0, 4, 4, "About"); { mnu5.setAlphabeticShortcut('e'); mnu5.setIcon(R.drawable.ic_launcher); } } private boolean MenuChoice(MenuItem item) { switch (item.getItemId()) { case 0: ClearCallLog(); return true; case 1: Toast.makeText(this, "You clicked on Item 2", Toast.LENGTH_LONG) .show(); return true; case 2: Toast.makeText(this, "You clicked on Item 3", Toast.LENGTH_LONG) .show(); return true; case 3: InsertCallLog(); CallLogListSetAdapte(); return true; case 4: Toast.makeText(this, "You clicked on Item 5", Toast.LENGTH_LONG) .show(); return true; case 5: Toast.makeText(this, "You clicked on Item 6", Toast.LENGTH_LONG) .show(); return true; case 6: Toast.makeText(this, "You clicked on Item 7", Toast.LENGTH_LONG) .show(); return true; } return false; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:stackFromBottom="false" android:transcriptMode="normal" /> </LinearLayout>
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25