Android学习之——操作SIM卡联系人
<uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/menu_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btnSelect" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看SIM卡联系人" /> <Button android:id="@+id/btnDelect" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除SIM卡联系人" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Demo" android:textSize="12sp" android:layout_centerInParent="true" /> </LinearLayout> </ScrollView>java文件:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // inflate the layout View view = inflater.inflate(R.layout.fragment_text, null); textView = (TextView) view.findViewById(R.id.textView); btnSelect = (Button) view.findViewById(R.id.btnSelect); btnDelete = (Button) view.findViewById(R.id.btnDelect); btnSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //我发现开了线程还是会阻塞UI线程,不晓得是什么原因,难道?我需要用AsyncTask?或者是Handler来处理。 //因为忙着玩新手机,所以我暂时也不深究了,哈哈,慢慢来,四儿子想了它一年多了。 new Thread(new Runnable() { @Override public void run() { SimQuery();//执行查询操作 } }).start(); textView.setText(messageString); } }); btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { SimDelete();//执行删除操作 } }).start(); textView.setText("删除成功!"); } }); return view; }
相关查询联系人和删除联系人的操作:
private void SimQuery() { //SIM的provider是IccProvider,IccProvider的Uri是content://icc/adn Uri uri = Uri.parse("content://icc/adn"); Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null); Log.v("MAIN", "-----------" + cursor.getCount()); while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex(People._ID)); String name = cursor.getString(cursor.getColumnIndex(People.NAME)); String phoneNumber = cursor.getString(cursor .getColumnIndex(People.NUMBER)); messageString += "id:" + id + " name:" + name + " phoneNumber:" + phoneNumber + "\n\r"; Log.v("MAIN", "--->>>_id: " + id + "--->>>name:" + name + "--->>>phone number:" + phoneNumber + "\n\r"); } cursor.close();//注意一定要close } private void SimDelete() { Uri uri = Uri.parse("content://icc/adn"); Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null); Log.v("MAIN", "------------->>>>>" + cursor.getCount()); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(People.NAME)); String phoneNumber = cursor.getString(cursor .getColumnIndex(People.NUMBER)); String where = "tag=‘" + name + "‘"; where += " AND number=‘" + phoneNumber + "‘"; getActivity().getContentResolver().delete(uri, where, null); } cursor.close();//注意一定要close }关于修改联系人和插入联系人的操作,大家就请移步链接吧,如果我有这个需要,我就会写出来的。
相关学习链接:http://my.eoe.cn/kris/archive/1605.html http://my.eoe.cn/kris/archive/1688.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。