免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 14319 | 回复: 10
打印 上一主题 下一主题

Android上读取SIM卡上联系人信息[求助] [复制链接]

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-01 16:49 |只看该作者 |倒序浏览
本帖最后由 renxiao2003 于 2010-04-07 16:21 编辑

哪位朋友有Android平台(1.6)上读取SIM卡上通讯录信息的示例代码?跟上!

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2010-04-02 08:47 |只看该作者
难道没有人会吗?

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
3 [报告]
发表于 2010-04-07 16:23 |只看该作者
本帖最后由 renxiao2003 于 2010-04-11 21:46 编辑

看来这里没人做这个啊。把人家的代码反编译了(把dex反成ddx),看出点门道。
然后自己做了一个例子就出来了。其中取SIM卡的联系人信息的代码部分如下:

  1. private Cursor getContacts() {
  2.                 // Run query
  3.                 Uri uri = Uri.parse("content://icc/adn");//Contacts.People.CONTENT_URI;
  4. //                Uri uri = Uri.parse("content://sim/adn");
  5.                 String[] projection = new String[] { "name",
  6.                                 "phone" };
  7.                 String selection = null;
  8.                 String[] selectionArgs = null;
  9.                 String sortOrder = "name"//Contacts.People.NAME// Contacts.PeopleColumns.DISPLAY_NAME
  10.                                 + " COLLATE LOCALIZED ASC";

  11.                 return managedQuery(uri, projection, selection, selectionArgs,
  12.                                 sortOrder);
  13.         }
复制代码
然后循环处理返回的游标就OK了。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
4 [报告]
发表于 2010-04-08 11:39 |只看该作者
在Android1.6及以前的版本中查询手机中的电话用android.provider.Contacts.People.CONTENT_URI
在Android2.0及以后(目前到了2.1,再往后不能保证无变化)用android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI
进行查询。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
5 [报告]
发表于 2010-04-11 21:44 |只看该作者

Android2.0中获取手机里联系人的方法

本帖最后由 renxiao2003 于 2010-04-11 21:46 编辑
  1. public void getContact(){   
  2.      //获得所有的联系人   
  3.     Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);   
  4.     //循环遍历   
  5.     if (cur.moveToFirst()) {   
  6.         int idColumn  = cur.getColumnIndex(ContactsContract.Contacts._ID);   
  7.            
  8.         int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);   
  9.         do {   
  10.             //获得联系人的ID号   
  11.            String contactId = cur.getString(idColumn);   
  12.            //获得联系人姓名   
  13.            String disPlayName = cur.getString(displayNameColumn);   
  14.            //查看该联系人有多少个电话号码。如果没有这返回值为0   
  15.            int phoneCount = cur.getInt(cur   
  16.                     .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));   
  17.            if(phoneCount>0){   
  18.                //获得联系人的电话号码   
  19.                Cursor phones = getContentResolver().query(   
  20.                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
  21.                         null,   
  22.                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID   
  23.                                 + " = " + contactId, null, null);   
  24.                if(phones.moveToFirst()){   
  25.                    do{   
  26.                        //遍历所有的电话号码   
  27.                        String phoneNumber= phones.getString(phones     
  28.                                 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));   
  29.                        System.out.println(phoneNumber);   
  30.                    }while(phones.moveToNext());   
  31.                }   
  32.               
  33.            }   
  34.   
  35.            } while (cur.moveToNext());   
  36.   
  37.     }   
  38.     }
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
6 [报告]
发表于 2010-04-11 21:45 |只看该作者
本帖最后由 renxiao2003 于 2010-04-11 21:47 编辑

在联系人的电话号码中有很多种,如果只想获得手机号码。代码如下:

Java代码

  1. Cursor phones = mContext.getContentResolver().query(   
  2.         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
  3.         null,   
  4.         ContactsContract.CommonDataKinds.Phone.CONTACT_ID   
  5.                 + " = " + contactId +" and "+ContactsContract.CommonDataKinds.Phone.TYPE+"="+ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, null, null);  

  6.   Cursor phones = mContext.getContentResolver().query(
  7.                                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  8.                                 null,
  9.                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID
  10.                                                 + " = " + contactId +" and "+ContactsContract.CommonDataKinds.Phone.TYPE+"="+ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, null, null);
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
7 [报告]
发表于 2010-04-15 08:34 |只看该作者
唉。做项目遇到这个东西,整得我不爽。找好多资料都没用。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
8 [报告]
发表于 2010-04-21 16:55 |只看该作者

RE: Android上读取SIM卡上联系人信息[求助] SimContacts.java

  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */

  16. package com.android.phone;

  17. import android.app.ProgressDialog;
  18. import android.content.ContentResolver;
  19. import android.content.ContentUris;
  20. import android.content.ContentValues;
  21. import android.content.DialogInterface;
  22. import android.content.Intent;
  23. import android.content.DialogInterface.OnCancelListener;
  24. import android.content.DialogInterface.OnClickListener;
  25. import android.database.Cursor;
  26. import android.database.DatabaseUtils;
  27. import android.net.Uri;
  28. import android.os.Bundle;
  29. import android.provider.Contacts;
  30. import android.provider.Contacts.People;
  31. import android.text.TextUtils;
  32. import android.util.Log;
  33. import android.view.ContextMenu;
  34. import android.view.KeyEvent;
  35. import android.view.Menu;
  36. import android.view.MenuItem;
  37. import android.view.View;
  38. import android.widget.AdapterView;
  39. import android.widget.CursorAdapter;
  40. import android.widget.ListView;
  41. import android.widget.SimpleCursorAdapter;
  42. import android.widget.TextView;

  43. /**
  44. * SIM Address Book UI for the Phone app.
  45. */
  46. public class SimContacts extends ADNList {
  47.     private static final int MENU_IMPORT_ONE = 1;
  48.     private static final int MENU_IMPORT_ALL = 2;
  49.     private ProgressDialog mProgressDialog;

  50.    
  51.     @Override
  52.     protected void onCreate(Bundle icicle) {
  53.         super.onCreate(icicle);
  54.         registerForContextMenu(getListView());
  55.     }

  56.     private class ImportAllThread extends Thread implements OnCancelListener, OnClickListener {
  57.         boolean mCanceled = false;
  58.         
  59.         public ImportAllThread() {
  60.             super("ImportAllThread");
  61.         }
  62.         
  63.         @Override
  64.         public void run() {
  65.             ContentValues map = new ContentValues();
  66.             ContentResolver cr = getContentResolver();
  67.             Object[] parsed = new Object[2];
  68.             
  69.             mCursor.moveToPosition(-1);
  70.             while (!mCanceled && mCursor.moveToNext()) {
  71.                 String name = mCursor.getString(0);
  72.                 String number = mCursor.getString(1);

  73.                 Uri personUrl = parseName(name, parsed);

  74.                 if (personUrl == null) {
  75.                     map.clear();
  76.                     map.put(Contacts.People.NAME, (String) parsed[0]);
  77.                     personUrl = People.createPersonInMyContactsGroup(cr, map);
  78.                     if (personUrl == null) {
  79.                         Log.e(TAG, "Error inserting person " + map);
  80.                         continue;
  81.                     }
  82.                 }

  83.                 map.clear();
  84.                 map.put(Contacts.People.Phones.NUMBER, number);
  85.                 map.put(Contacts.People.Phones.TYPE, (Integer) parsed[1]);
  86.                 Uri numberUrl = cr.insert(
  87.                         Uri.withAppendedPath(personUrl, Contacts.People.Phones.CONTENT_DIRECTORY),
  88.                         map);
  89.                
  90.                 mProgressDialog.incrementProgressBy(1);
  91.                 if (numberUrl == null) {
  92.                     Log.e(TAG, "Error inserting phone " + map + " for person " +
  93.                             personUrl + ", removing person");
  94.                     continue;
  95.                 }
  96.             }
  97.             
  98.             mProgressDialog.dismiss();

  99.             finish();
  100.         }

  101.         public void onCancel(DialogInterface dialog) {
  102.             mCanceled = true;
  103.         }

  104.         public void onClick(DialogInterface dialog, int which) {
  105.             mCanceled = true;
  106.             mProgressDialog.dismiss();
  107.         }
  108.     }

  109.     @Override
  110.     protected CursorAdapter newAdapter() {
  111.         return new SimpleCursorAdapter(this, R.layout.sim_import_list_entry, mCursor,
  112.                 new String[] { "name" }, new int[] { android.R.id.text1 });
  113.     }

  114.     @Override
  115.     protected Uri resolveIntent() {
  116.         Intent intent = getIntent();
  117.         intent.setData(Uri.parse("content://icc/adn"));
  118.         if (Intent.ACTION_PICK.equals(intent.getAction())) {
  119.             // "index" is 1-based
  120.             mInitialSelection = intent.getIntExtra("index", 0) - 1;
  121.         }
  122.         return intent.getData();
  123.     }

  124.     @Override
  125.     public boolean onCreateOptionsMenu(Menu menu) {
  126.         super.onCreateOptionsMenu(menu);
  127.         menu.add(0, MENU_IMPORT_ALL, 0, R.string.importAllSimEntries);
  128.         return true;
  129.     }

  130.     @Override
  131.     public boolean onOptionsItemSelected(MenuItem item) {
  132.         switch (item.getItemId()) {
  133.             case MENU_IMPORT_ALL:
  134.                 CharSequence title = getString(R.string.importAllSimEntries);
  135.                 CharSequence message = getString(R.string.importingSimContacts);

  136.                 ImportAllThread thread = new ImportAllThread();

  137.                 mProgressDialog = new ProgressDialog(this);
  138.                 mProgressDialog.setTitle(title);
  139.                 mProgressDialog.setMessage(message);
  140.                 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  141.                 mProgressDialog.setButton(getString(R.string.cancel), thread);
  142.                 mProgressDialog.setProgress(0);
  143.                 mProgressDialog.setMax(mCursor.getCount());
  144.                 mProgressDialog.show();
  145.                
  146.                 thread.start();
  147.                
  148.                 return true;
  149.         }
  150.         return super.onOptionsItemSelected(item);
  151.     }
  152.    
  153.     @Override
  154.     public boolean onContextItemSelected(MenuItem item) {
  155.         switch (item.getItemId()) {
  156.             case MENU_IMPORT_ONE:
  157.                 ContextMenu.ContextMenuInfo menuInfo = item.getMenuInfo();
  158.                 if (menuInfo instanceof AdapterView.AdapterContextMenuInfo) {
  159.                     int position = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
  160.                     importOne(position);
  161.                     return true;
  162.                 }
  163.         }
  164.         return super.onContextItemSelected(item);
  165.     }

  166.    
  167.     public void onCreateContextMenu(ContextMenu menu, View v,
  168.             ContextMenu.ContextMenuInfo menuInfo) {
  169.         if (menuInfo instanceof AdapterView.AdapterContextMenuInfo) {
  170.             AdapterView.AdapterContextMenuInfo itemInfo =
  171.                     (AdapterView.AdapterContextMenuInfo) menuInfo;
  172.             TextView textView = (TextView) itemInfo.targetView.findViewById(android.R.id.text1);
  173.             if (textView != null) {
  174.                 menu.setHeaderTitle(textView.getText());
  175.             }
  176.             menu.add(0, MENU_IMPORT_ONE, 0, R.string.importSimEntry);
  177.         }
  178.     }

  179.     @Override
  180.     public void onListItemClick(ListView l, View v, int position, long id) {
  181.         importOne(position);
  182.     }

  183.     private void importOne(int position) {
  184.         if (mCursor.moveToPosition(position)) {
  185.             String name = mCursor.getString(NAME_COLUMN);
  186.             String number = mCursor.getString(NUMBER_COLUMN);
  187.             Object[] parsed = new Object[2];
  188.             Uri personUrl = parseName(name, parsed);

  189.             Intent intent;
  190.             if (personUrl == null) {
  191.                 // Add a new contact
  192.                 intent = new Intent(Contacts.Intents.Insert.ACTION,
  193.                         Contacts.People.CONTENT_URI);
  194.                 intent.putExtra(Contacts.Intents.Insert.NAME, (String)parsed[0]);
  195.                 intent.putExtra(Contacts.Intents.Insert.PHONE, number);
  196.                 intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, ((Integer)parsed[1]).intValue());
  197.             } else {
  198.                 // Add the number to an existing contact
  199.                 intent = new Intent(Intent.ACTION_EDIT, personUrl);
  200.                 intent.putExtra(Contacts.Intents.Insert.PHONE, number);
  201.                 intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, ((Integer)parsed[1]).intValue());
  202.             }
  203.             startActivity(intent);
  204.         }
  205.     }

  206.     /**
  207.      * Parse the name looking for /W /H /M or /O at the end, signifying the type.
  208.      *
  209.      * @param name The name from the SIM card
  210.      * @param parsed slot 0 is filled in with the name, and slot 1 is filled
  211.      * in with the type
  212.      */
  213.     private Uri parseName(String name, Object[] parsed) {
  214.         // default to TYPE_MOBILE so you can send SMSs to the numbers
  215.         int type = Contacts.PhonesColumns.TYPE_MOBILE;

  216.         // Look for /W /H /M or /O at the end of the name signifying the type
  217.         int nameLen = name.length();
  218.         if (nameLen - 2 >= 0 && name.charAt(nameLen - 2) == '/') {
  219.             char c = Character.toUpperCase(name.charAt(nameLen - 1));
  220.             if (c == 'W') {
  221.                 type = Contacts.PhonesColumns.TYPE_WORK;
  222.             } else if (c == 'M') {
  223.                 type = Contacts.PhonesColumns.TYPE_MOBILE;
  224.             } else if (c == 'H') {
  225.                 type = Contacts.PhonesColumns.TYPE_HOME;
  226.             } else if (c == 'O') {
  227.                 type = Contacts.PhonesColumns.TYPE_MOBILE;
  228.             }
  229.             name = name.substring(0, nameLen - 2);
  230.         }
  231.         parsed[0] = name;
  232.         parsed[1] = type;

  233.         StringBuilder where = new StringBuilder(Contacts.People.NAME);
  234.         where.append('=');
  235.         DatabaseUtils.appendEscapedSQLString(where, name);
  236.         Uri url = null;
  237.         Cursor c = getContentResolver().query(Contacts.People.CONTENT_URI,
  238.                 new String[] {Contacts.People._ID},
  239.                 where.toString(), null, null);
  240.         if (c != null) {
  241.             if (c.moveToFirst()) {
  242.                 url = ContentUris.withAppendedId(Contacts.People.CONTENT_URI, c.getLong(0));
  243.             }
  244.             c.deactivate();
  245.         }
  246.         return url;
  247.     }

  248.     @Override
  249.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  250.         switch (keyCode) {
  251.             case KeyEvent.KEYCODE_CALL: {
  252.                 if (mCursor != null && mCursor.moveToPosition(getSelectedItemPosition())) {
  253.                     String number = mCursor.getString(NUMBER_COLUMN);
  254.                     if (number == null || !TextUtils.isGraphic(number)) {
  255.                         // There is no number entered.
  256.                         //TODO play error sound or something...
  257.                         return true;
  258.                     }
  259.                     Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
  260.                             Uri.fromParts("tel", number, null));
  261.                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
  262.                                           | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  263.                     startActivity(intent);
  264.                     finish();
  265.                     return true;
  266.                 }
  267.             }
  268.         }
  269.         return super.onKeyDown(keyCode, event);
  270.     }
  271. }


复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
9 [报告]
发表于 2010-04-21 16:56 |只看该作者

RE: Android上读取SIM卡上联系人信息[求助] ADNList.java

  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */

  16. package com.android.phone;

  17. import static android.view.Window.PROGRESS_VISIBILITY_OFF;
  18. import static android.view.Window.PROGRESS_VISIBILITY_ON;

  19. import android.app.ListActivity;
  20. import android.content.AsyncQueryHandler;
  21. import android.content.ContentResolver;
  22. import android.content.Intent;
  23. import android.database.Cursor;
  24. import android.net.Uri;
  25. import android.os.Bundle;
  26. import android.util.Log;
  27. import android.view.Window;
  28. import android.widget.CursorAdapter;
  29. import android.widget.SimpleCursorAdapter;
  30. import android.widget.TextView;

  31. /**
  32. * ADN List activity for the Phone app.
  33. */
  34. public class ADNList extends ListActivity {
  35.     protected static final String TAG = "ADNList";
  36.     protected static final boolean DBG = false;

  37.     private static final String[] COLUMN_NAMES = new String[] {
  38.         "name",
  39.         "number"
  40.     };
  41.    

  42.     protected static final int NAME_COLUMN = 0;
  43.     protected static final int NUMBER_COLUMN = 1;

  44.     private static final int[] VIEW_NAMES = new int[] {
  45.         android.R.id.text1,
  46.         android.R.id.text2
  47.     };

  48.     protected static final int QUERY_TOKEN = 0;
  49.     protected static final int INSERT_TOKEN = 1;
  50.     protected static final int UPDATE_TOKEN = 2;
  51.     protected static final int DELETE_TOKEN = 3;


  52.     protected QueryHandler mQueryHandler;
  53.     protected CursorAdapter mCursorAdapter;
  54.     protected Cursor mCursor = null;

  55.     private TextView mEmptyText;

  56.     protected int mInitialSelection = -1;

  57.     @Override
  58.     protected void onCreate(Bundle icicle) {
  59.         super.onCreate(icicle);
  60.         getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  61.         setContentView(R.layout.adn_list);
  62.         mEmptyText = (TextView) findViewById(android.R.id.empty);
  63.         mQueryHandler = new QueryHandler(getContentResolver());
  64.     }

  65.     @Override
  66.     protected void onResume() {
  67.         super.onResume();
  68.         query();
  69.     }

  70.     @Override
  71.     protected void onStop() {
  72.         super.onStop();
  73.         if (mCursor != null) {
  74.             mCursor.deactivate();
  75.         }
  76.     }

  77.     protected Uri resolveIntent() {
  78.         Intent intent = getIntent();
  79.         if (intent.getData() == null) {
  80.             intent.setData(Uri.parse("content://icc/adn"));
  81.         }

  82.         return intent.getData();
  83.     }

  84.     private void query() {
  85.         Uri uri = resolveIntent();
  86.         if (DBG) log("query: starting an async query");
  87.         mQueryHandler.startQuery(QUERY_TOKEN, null, uri, COLUMN_NAMES,
  88.                 null, null, null);
  89.         displayProgress(true);
  90.     }

  91.     private void reQuery() {
  92.         query();
  93.     }

  94.     private void setAdapter() {
  95.         // NOTE:
  96.         // As it it written, the positioning code below is NOT working.
  97.         // However, this current non-working state is in compliance with
  98.         // the UI paradigm, so we can't really do much to change it.
  99.         
  100.         // In the future, if we wish to get this "positioning" correct,
  101.         // we'll need to do the following:
  102.         //   1. Change the layout to in the cursor adapter to:
  103.         //     android.R.layout.simple_list_item_checked
  104.         //   2. replace the selection / focus code with:
  105.         //     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  106.         //     getListView().setItemChecked(mInitialSelection, true);
  107.         
  108.         // Since the positioning is really only useful for the dialer's
  109.         // SpecialCharSequence case (dialing '2#' to get to the 2nd
  110.         // contact for instance), it doesn't make sense to mess with
  111.         // the usability of the activity just for this case.
  112.         
  113.         // These artifacts include:
  114.         //  1. UI artifacts (checkbox and highlight at the same time)
  115.         //  2. Allowing the user to edit / create new SIM contacts when
  116.         //    the user is simply trying to retrieve a number into the d
  117.         //    dialer.
  118.         
  119.         if (mCursorAdapter == null) {
  120.             mCursorAdapter = newAdapter();

  121.             setListAdapter(mCursorAdapter);
  122.         } else {
  123.             mCursorAdapter.changeCursor(mCursor);
  124.         }

  125.         if (mInitialSelection >=0 && mInitialSelection < mCursorAdapter.getCount()) {
  126.             setSelection(mInitialSelection);
  127.             getListView().setFocusableInTouchMode(true);
  128.             boolean gotfocus = getListView().requestFocus();
  129.         }
  130.     }

  131.     protected CursorAdapter newAdapter() {
  132.         return new SimpleCursorAdapter(this,
  133.                     android.R.layout.simple_list_item_2,
  134.                     mCursor, COLUMN_NAMES, VIEW_NAMES);
  135.     }

  136.     private void displayProgress(boolean flag) {
  137.         if (DBG) log("displayProgress: " + flag);
  138.         mEmptyText.setText(flag ? R.string.simContacts_emptyLoading: R.string.simContacts_empty);
  139.         getWindow().setFeatureInt(
  140.                 Window.FEATURE_INDETERMINATE_PROGRESS,
  141.                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
  142.     }

  143.     private class QueryHandler extends AsyncQueryHandler {
  144.         public QueryHandler(ContentResolver cr) {
  145.             super(cr);
  146.         }

  147.         @Override
  148.         protected void onQueryComplete(int token, Object cookie, Cursor c) {
  149.             if (DBG) log("onQueryComplete: cursor.count=" + c.getCount());
  150.             mCursor = c;
  151.             setAdapter();
  152.             displayProgress(false);
  153.         }

  154.         @Override
  155.         protected void onInsertComplete(int token, Object cookie,
  156.                                         Uri uri) {
  157.             if (DBG) log("onInsertComplete: requery");
  158.             reQuery();
  159.         }

  160.         @Override
  161.         protected void onUpdateComplete(int token, Object cookie, int result) {
  162.             if (DBG) log("onUpdateComplete: requery");
  163.             reQuery();
  164.         }

  165.         @Override
  166.         protected void onDeleteComplete(int token, Object cookie, int result) {
  167.             if (DBG) log("onDeleteComplete: requery");
  168.             reQuery();
  169.         }
  170.     }

  171.     protected void log(String msg) {
  172.         Log.d(TAG, "[ADNList] " + msg);
  173.     }
  174. }
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
10 [报告]
发表于 2010-04-21 16:57 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP