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.         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值