Android自学之路--ContentProvider
A)MainActivity部分的代码
1 package com.example.cms.systemcontentprovider; 2 3 import android.app.ExpandableListActivity; 4 import android.content.ContentUris; 5 import android.content.ContentValues; 6 import android.database.Cursor; 7 import android.net.Uri; 8 import android.provider.ContactsContract; 9 import android.support.v7.app.AlertDialog; 10 import android.support.v7.app.AppCompatActivity; 11 import android.os.Bundle; 12 import android.view.Gravity; 13 import android.view.SurfaceHolder; 14 import android.view.View; 15 import android.view.ViewGroup; 16 import android.widget.AbsListView; 17 import android.widget.BaseExpandableListAdapter; 18 import android.widget.Button; 19 import android.widget.EditText; 20 import android.widget.ExpandableListAdapter; 21 import android.widget.ExpandableListView; 22 import android.widget.TextView; 23 import android.widget.Toast; 24 25 import org.w3c.dom.Text; 26 27 import java.util.ArrayList; 28 29 public class MainActivity extends AppCompatActivity { 30 private Button searchButton; 31 private Button addButton; 32 private EditText inputname; 33 private EditText inputnummber; 34 private EditText inputemail; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 searchButton= (Button) findViewById(R.id.searchButton); 41 addButton= (Button) findViewById(R.id.addButton); 42 inputname= (EditText) findViewById(R.id.inputname); 43 inputnummber= (EditText) findViewById(R.id.inputnumber); 44 inputemail= (EditText) findViewById(R.id.inputemail); 45 46 searchButton.setOnClickListener(new View.OnClickListener() { 47 @Override 48 public void onClick(View v) { 49 //定义两个List来封装里联系人信息 指定联系人的电话号码 Email等详情 50 final ArrayList<String> names=new ArrayList<String>(); 51 final ArrayList<ArrayList<String>> details=new ArrayList<ArrayList<String>>(); 52 //使用ContentResolver获取系统所有联系人 53 Cursor cursor=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 54 //遍历查询结果 55 while(cursor.moveToNext()){ 56 //获取联系人的姓名 57 String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 58 names.add(name); 59 //获取联系人对应的ID号 60 String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 61 //查找该联系人的电话号码 62 Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null); 63 //将每个联系人的信息封装成一个LIST 64 ArrayList<String>detail=new ArrayList<String>(); 65 //可能一个联系人都多个电话号码 所以需要遍历 66 while(phones.moveToNext()){ 67 //获取查询结果中电话号码列中的数据 68 String phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 69 detail.add("电话号码:"+phoneNumber); 70 } 71 phones.close(); 72 //查找该联系人的Email信息 73 Cursor emails=getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+contactId,null,null); 74 while(emails.moveToNext()){ 75 String emailsAddress=emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 76 detail.add("邮件地址:"+emailsAddress); 77 } 78 emails.close(); 79 details.add(detail); 80 } 81 cursor.close(); 82 83 //加载布局文件 84 View resultDialog=getLayoutInflater().inflate(R.layout.result,null); 85 ExpandableListView list= (ExpandableListView) resultDialog.findViewById(R.id.list); 86 87 ExpandableListAdapter adapter=new BaseExpandableListAdapter() { 88 //返回所有分组的数目(即联系人的数目) 89 @Override 90 public int getGroupCount() { 91 return names.size(); 92 } 93 94 //返回groupPosition对应位置的联系人底下包含的电话号码数和邮件数 95 @Override 96 public int getChildrenCount(int groupPosition) { 97 return details.get(groupPosition).size(); 98 } 99 100 //返回groupPosition对应的联系人分组 101 @Override 102 public Object getGroup(int groupPosition) { 103 return names.get(groupPosition); 104 } 105 //返回groupPosition对应联系人的childPosition位置的对应信息 106 @Override 107 public Object getChild(int groupPosition, int childPosition) { 108 return details.get(groupPosition).get(childPosition); 109 } 110 @Override 111 public long getGroupId(int groupPosition) { 112 return groupPosition; 113 } 114 115 @Override 116 public long getChildId(int groupPosition, int childPosition) { 117 return childPosition; 118 } 119 private TextView getTextView(){ 120 TextView textView=new TextView(MainActivity.this); 121 AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64); 122 textView.setLayoutParams(lp); 123 textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT); 124 textView.setPadding(36,0,0,0); 125 textView.setTextSize(20); 126 return textView; 127 } 128 //决定每个联系人分组选项外观的方法 129 @Override 130 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 131 TextView textView=getTextView(); 132 textView.setText(getGroup(groupPosition).toString()); 133 return textView; 134 } 135 //决定每个子选项外观的方法 136 @Override 137 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 138 TextView textView=getTextView(); 139 textView.setText(getChild(groupPosition,childPosition).toString()); 140 return textView; 141 } 142 143 @Override 144 public boolean hasStableIds() { 145 return true; 146 } 147 148 @Override 149 public boolean isChildSelectable(int groupPosition, int childPosition) { 150 return true; 151 } 152 }; 153 list.setAdapter(adapter); 154 new AlertDialog.Builder(MainActivity.this).setView(resultDialog).setPositiveButton("确定",null).show(); 155 } 156 }); 157 158 //为add添加联系人按钮绑定监听事件 159 addButton.setOnClickListener(new View.OnClickListener() { 160 @Override 161 public void onClick(View v) { 162 //获取用户输入的联系人信息 163 String name=inputname.getText().toString(); 164 String number=inputnummber.getText().toString(); 165 String email=inputemail.getText().toString(); 166 //创建一个空的ContentValues 然后向RawContacts.CONTENT_URI执行一个空值插入 这样就能返回一个rawContactId 从而得到插入的位置 167 ContentValues values=new ContentValues();
168 Uri rawContactUri=getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI,values); 169 long rawContactId= ContentUris.parseId(rawContactUri); 170 values.clear(); 171 //设置联系人姓名模块 172 values.put(ContactsContract.Data.RAW_CONTACT_ID,rawContactId); 173 values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); 174 values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,name); 175 getContentResolver().insert(ContactsContract.Data.CONTENT_URI,values); //向联系人URI添加联系人名字 176 177 values.clear(); 178 //设置联系人电话号码模块 179 values.put(ContactsContract.Data.RAW_CONTACT_ID,rawContactId); 180 values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 181 values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,number); 182 values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);//设置电话号码类型 183 getContentResolver().insert(ContactsContract.Data.CONTENT_URI,values); 184 185 values.clear(); 186 //设置联系人邮件地址模块 187 values.put(ContactsContract.Data.RAW_CONTACT_ID,rawContactId); 188 values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); 189 values.put(ContactsContract.CommonDataKinds.Email.DATA,email); 190 values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK); 191 getContentResolver().insert(ContactsContract.Data.CONTENT_URI,values); 192 Toast.makeText(MainActivity.this,"添加联系人成功", Toast.LENGTH_SHORT).show(); 193 } 194 }); 195 } 196 }
B)activity_main.xml 布局文件代码
<?xml version="1.0" encoding="utf-8"?> <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="com.example.cms.systemcontentprovider.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/searchButton" android:text="查看所有联系人" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputname" android:hint="请输入联系人的姓名" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputnumber" android:hint="请输入联系人的电话号码" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputemail" android:hint="请输入联系人的Email" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/addButton" android:layout_below="@+id/searchButton" android:text="添加联系人" /> </LinearLayout>
C)result.xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ExpandableListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list" ></ExpandableListView> </LinearLayout>
D)AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.cms.systemcontentprovider"> <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:exported="true" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>