Android的SIM卡名片导入流程

ContactsListActivity 在ContactsListActivity里创建了一个名片导入的菜单。
  1. // SIM import
  2. Intent importIntent = new Intent(Intent.ACTION_VIEW);
  3. importIntent.setType("vnd.android.cursor.item/sim-contact");
  4. importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
  5. menu.add(0, 0, 0, R.string.importFromSim)
  6. .setIcon(R.drawable.ic_menu_import_contact)
  7. .setIntent(importIntent);
复制代码
很明显可以看出名片应用程序只是启动了电话里面的SimContacts: ADNList SimContacts是从ADNList继承过来的,有必要先看下ADNList。它是一个ListActivity,用来显示SIM卡中的名片。 先查询SIM卡中的名片:
  1. protected Uri resolveIntent() {
  2. Intent intent = getIntent();
  3. if (intent.getData() == null) {
  4. intent.setData(Uri.parse("content://sim/adn"));
  5. }

  6. return intent.getData();
  7. }

  8. private void query() {
  9. Uri uri = resolveIntent();
  10. if (DBG) log("query: starting an async query");
  11. mQueryHandler.startQuery(QUERY_TOKEN, null, uri, COLUMN_NAMES,
  12. null, null, null);
  13. displayProgress(true);
  14. }
复制代码


mQueryHandler是AsyncQueryHandler的子类,当查询完成时,会调用onQueryComplete去更新SIM卡名片列表和进度状态:
  1. protected void onQueryComplete(int token, Object cookie, Cursor c) {
  2. if (DBG) log("onQueryComplete: cursor.count=" + c.getCount());
  3. mCursor = c;
  4. setAdapter();
  5. displayProgress(false);
  6. }
复制代码


SimContacts 再回到SimContacts,从SIM卡里查询名片是ADNList做的,SimContacts则主要是负责导入SIM卡名片到手机名片里: 选择某条名片导入,会给用户编辑的机会,由名片应用程序来负责:
  1. private void importOne(int position) {
  2. if (mCursor.moveToPosition(position)) {
  3. String name = mCursor.getString(NAME_COLUMN);
  4. String number = mCursor.getString(NUMBER_COLUMN);
  5. Object[] parsed = new Object[2];
  6. Uri personUrl = parseName(name, parsed);

  7. Intent intent;
  8. if (personUrl == null) {
  9. // Add a new contact
  10. intent = new Intent(Contacts.Intents.Insert.ACTION,
  11. Contacts.People.CONTENT_URI);
  12. intent.putExtra(Contacts.Intents.Insert.NAME, (String)parsed[0]);
  13. intent.putExtra(Contacts.Intents.Insert.PHONE, number);
  14. intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, ((Integer)parsed[1]).intValue());
  15. } else {
  16. // Add the number to an existing contact
  17. intent = new Intent(Intent.ACTION_EDIT, personUrl);
  18. }
  19. startActivity(intent);
  20. }
  21. }
复制代码


如果是全部导入,则直接通过ContentResolver插入进去,这里由一个独立的线程负责:
  1. public void run() {
  2. ContentValues map = new ContentValues();
  3. ContentResolver cr = getContentResolver();
  4. Object[] parsed = new Object[2];

  5. mCursor.moveToPosition(-1);
  6. while (!mCanceled && mCursor.moveToNext()) {
  7. String name = mCursor.getString(0);
  8. String number = mCursor.getString(1);

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

  10. if (personUrl == null) {
  11. map.clear();
  12. map.put(Contacts.People.NAME, (String) parsed[0]);
  13. personUrl = People.createPersonInMyContactsGroup(cr, map);
  14. if (personUrl == null) {
  15. Log.e(TAG, "Error inserting person " + map);
  16. continue;
  17. }
  18. }

  19. map.clear();
  20. map.put(Contacts.People.Phones.NUMBER, number);
  21. map.put(Contacts.People.Phones.TYPE, (Integer) parsed[1]);
  22. Uri numberUrl = cr.insert(
  23. Uri.withAppendedPath(personUrl, Contacts.People.Phones.CONTENT_DIRECTORY),
  24. map);

  25. mProgressDialog.incrementProgressBy(1);
  26. if (numberUrl == null) {
  27. Log.e(TAG, "Error inserting phone " + map + " for person " +
  28. personUrl + ", removing person");
  29. continue;
  30. }
  31. }

  32. mProgressDialog.dismiss();

  33. finish();
  34. }

原文: http://www.apkbus.com/android-2031-1-1.html

更多相关文章

  1. Android系统进程Zygote启动过程的源代码分析(2)
  2. Android之——代码混淆
  3. Android源代码下载指南(图解)
  4. Android系统进程Zygote启动过程的源代码分析
  5. Android布局--相对布局,RTL,用代码实现布局
  6. Android:高效的Android代码编写

随机推荐

  1. Android(安卓)TextSwitcher通知公告自动
  2. android通讯录根据手机号码查询姓名
  3. android 一键锁屏
  4. android xml 解析 修改
  5. android使用自定义属性
  6. Android五大布局(二)——RelativeLayout、T
  7. 推荐一系列优秀的Android开发源码
  8. ExpandableListView用法实例
  9. Android日常知识收集与总结之Android的Di
  10. Android UnitTest