天冷了,老夫要把伙食搞上去,這不最近在軟件園二樓吃,伙食15塊,槓槓的。
java
美包包,不說了,進入正題。今天老夫要講的是讀取聯繫人,其實我作這個的初衷是想作一個短信攔截,電話攔截的功能。android
咱們先看一下界面,仍是不錯的,挺絢麗的。app
OK,咱們一看就知道,這又是一個ListView。目前的功能是當你在複選框打鉤,點擊後面的撥號就會將電話打出去。若是不打鉤,電話則不會撥出去。OK,咱們先看看頁面佈局ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/contactListView" android:descendantFocusability="blocksDescendants" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="@color/teal" android:dividerHeight="1dp"> </ListView> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btnSelAll" android:text="@string/btnSelAll" android:textColor="@color/teal" android:textSize="14dp" android:textStyle="bold" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent"></Button> <Button android:id="@+id/btnInverseSel" android:text="@string/btnSelInverse" android:textColor="@color/teal" android:layout_marginLeft="1dp" android:layout_weight="1" android:textSize="14dp" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="fill_parent"></Button> <Button android:id="@+id/btnSet" android:text="@string/btnSet" android:layout_weight="1" android:textColor="@color/teal" android:layout_marginLeft="1dp" android:textSize="14dp" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="fill_parent"></Button> </LinearLayout> </LinearLayout>
咱們再看看ListView要加載的模版佈局
<?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="wrap_content" android:id="@+id/contactTemplate"> <TableLayout android:id="@+id/tabContatMain" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="2" android:shrinkColumns="2" android:padding="3dip"> <TableRow> <CheckBox android:id="@+id/chkContactUser" android:layout_gravity="center_vertical"></CheckBox> <ImageView android:id="@+id/imgContactPhoto" android:layout_gravity="center_vertical" android:scaleType="fitCenter"></ImageView> <TextView android:id="@+id/txtContactName" android:layout_marginLeft="10dp" android:layout_gravity="center_vertical" android:textColor="@color/teal1"></TextView> <TextView android:id="@+id/txtContactTelNumber" android:layout_marginLeft="4dp" android:gravity="right" android:layout_gravity="center_vertical" android:textColor="@color/yellow"></TextView> <Button android:id="@+id/btnDail" android:text="@string/btnDail" android:textSize="10dp" android:layout_marginLeft="10dp" android:width="60dp" android:drawableRight="@drawable/dail" android:layout_gravity="center_vertical" android:textColor="@color/purplered"></Button> </TableRow> </TableLayout> </LinearLayout>
依然是TableLayout佈局,咱們設置它的收縮列爲第二列,伸展列也是第二列。這樣若是第二列不夠顯示則會收縮。OK,咱們看一下後臺代碼測試
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.punchinalarm); owner = this; btnSelAll = (Button) this.findViewById(R.id.btnSelAll); btnSelInverse = (Button) this.findViewById(R.id.btnInverseSel); btnSet = (Button) this.findViewById(R.id.btnSet); contactUserListView = (ListView) this .findViewById(R.id.contactListView); dataList = new ArrayList<Map<String, Object>>(); this.InitData(); }
在OnCreate方法中,咱們初始化數據。ui
private void InitData() { String phoneUserName = null; String phoneNumber = null; Long contactId = null; Long photoId = null; Map<String, Object> dataMap = null; ContentResolver resolver = this.getApplicationContext() .getContentResolver(); /* * 獲取Sim卡聯繫人 Uri uri = Uri.parse("content://icc/adn"); */ Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONE_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { dataMap = new HashMap<String, Object>(); phoneUserName = phoneCursor.getString(0); phoneNumber = phoneCursor.getString(1); photoId = phoneCursor.getLong(2); contactId = phoneCursor.getLong(3); if (phoneNumber == null || phoneNumber.trim().length() < 11) { continue; } Bitmap contactPhoto = null; if (photoId > 0) { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, contactId); InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(resolver, uri); contactPhoto = BitmapFactory.decodeStream(input); } else { contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.usersmall); } dataMap.put("UserName", phoneUserName); dataMap.put("UserPhoneNumber", phoneNumber); dataMap.put("UserPhoto", contactPhoto); dataList.add(dataMap); } if (dataList != null && dataList.size() > 0) { customAdapter simpleAdapter = new customAdapter(this, dataList, R.layout.contactdetailtemplate, new String[] { "UserPhoto", "UserName", "UserPhoneNumber" }, new int[] { R.id.chkContactUser, R.id.imgContactPhoto, R.id.txtContactName, R.id.txtContactTelNumber, R.id.btnDail }); this.contactUserListView.setAdapter(simpleAdapter); } } }
咱們知道外界的程序經過ContentResolver接口能夠訪問ContentProvider提供的數據,在Activity當中經過getContentResolver()能夠獲得當前應用的 ContentResolver實例。由於通信錄和短消息均可以通過接口訪問,因此咱們就能夠經過getContentResolver訪問SIM卡和手機中的聯繫人信息。this
咱們主要到下面的這句spa
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONE_PROJECTION, null, null, null);
經過接口查詢,咱們會獲得一個Cursor。經過查看Cursor的定義,咱們發現它是個抽象類.net
public abstract interface android.database.Cursor
咱們發現它提供了一些方法,以下
因而可知,它是一個既能夠前進又能夠後退的無向遊標,相似於SqlServer中的遊標。這樣的話咱們不管是讀取聯繫人信息,仍是讀取短消息,均可以隨時定位遊標。
在上面咱們看到Query的幾個參數,Phone.Content_URI,獲取聯繫人的時候須要去這個URI去取數據。其實這裏的Phone.Content_URI的值是content://com.android.contacts/contacts。它的定義以下
public static final android.net.Uri CONTENT_URI;
ok,咱們拿到聯繫人以後,咱們進行循環,遊標下移,拿出全部的有電話號碼的聯繫人的數據。由於咱們傳入的PHONE_PROJECTION的順序是姓名,電話號碼,照片ID,以及一個ContactID。因此咱們看到取數據的順序以下
phoneUserName = phoneCursor.getString(0); phoneNumber = phoneCursor.getString(1); photoId = phoneCursor.getLong(2); contactId = phoneCursor.getLong(3);
拿到PhotoID以後,咱們判斷是否大於0,若是大於0,咱們會獲取用戶圖像。
獲取用戶圖像的時候,先經過下面的代碼將URI和參數鏈接起來
ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, contactId)
其實這個相似於Get方式的API,好比ContactUser/100,意思是獲取編號爲100的人的信息。
OK,URI構造好以後,咱們獲取圖像
InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(resolver, uri); contactPhoto = BitmapFactory.decodeStream(input);
最後加入List<Map<String,Object>>,對ListView運用適配器。
class customAdapter extends BaseAdapter { private List<Map<String, Object>> dataList; private LayoutInflater mInflater; private Context context; private String[] keyString; private int[] valueViewID; Holder holder; public customAdapter(Context context, List<Map<String, Object>> dataList, int resource, String[] from, int[] to) { this.dataList = dataList; this.context = context; mInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); keyString = new String[from.length]; valueViewID = new int[to.length]; System.arraycopy(from, 0, keyString, 0, from.length); System.arraycopy(to, 0, valueViewID, 0, to.length); } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } public void removeItem(int position) { dataList.remove(position); this.notifyDataSetChanged(); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView != null) { holder = (Holder) convertView.getTag(); } else { convertView = mInflater.inflate(R.layout.contactdetailtemplate, null); holder = new Holder(); holder.chkContactUser = (CheckBox) convertView .findViewById(valueViewID[0]); holder.imgUserPhoto = (ImageView) convertView .findViewById(valueViewID[1]); holder.labUserName = (TextView) convertView .findViewById(valueViewID[2]); holder.labPhoneNumber = (TextView) convertView .findViewById(valueViewID[3]); holder.btnDail = (Button) convertView .findViewById(valueViewID[4]); convertView.setTag(holder); } Map<String, Object> appInfo = dataList.get(position); if (appInfo != null) { String userName = appInfo.get(keyString[1]).toString(); String userPhoneNumber = appInfo.get(keyString[2]) == null ? "" : appInfo.get(keyString[2]).toString(); Bitmap userPhoto = (Bitmap) appInfo.get(keyString[0]); holder.labUserName.setText(userName); holder.labPhoneNumber.setText(userPhoneNumber); holder.imgUserPhoto.setImageBitmap(userPhoto); holder.btnDail.setOnClickListener(new ViewButtonListener( position, holder.chkContactUser)); } return convertView; } } class Holder { public TextView labUserName; public TextView labPhoneNumber; public ImageView imgUserPhoto; public Button btnDail; public CheckBox chkContactUser; }
這裏,其實很簡單,咱們就是拿到控件,而後根據Position,拿到List中的某行數據,而後賦值。
最後咱們看看按鈕的Click事件,咱們傳入了Position和每行的CheckBox。
class ViewButtonListener implements OnClickListener { private int position; Object phoneNumber; CheckBox chkContactUser; ViewButtonListener(int position,CheckBox chkContactUser) { this.position = position; this.phoneNumber = dataList.get(position).get("UserPhoneNumber"); this.chkContactUser= chkContactUser; } @Override public void onClick(View view) { int vid = view.getId(); if (vid == R.id.btnDail&&chkContactUser.isChecked()) { Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri .parse("tel:" + phoneNumber)); startActivity(dialIntent); } } }
在OnClick中,咱們判斷若是是撥號按鈕而且列頭的CheckBox是勾選的,則會撥號,不然不會撥號。OK,最後咱們但願在按回退鍵的時候,彈出是否退出的提示
ok,代碼以下
public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) { dialog(); return true; } return true; } protected void dialog() { AlertDialog.Builder builder = new Builder(punchinalarm.this); builder.setMessage("肯定要退出嗎?"); builder.setTitle("提示"); builder.setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); android.os.Process.killProcess(android.os.Process.myPid()); } }); builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }
最後,哥們的博客是貨真價實,小米3測試機。