日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【Android Developers Training】 100. 使用Int

系統 2548 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。

原文鏈接: http://developer.android.com/training/contacts-provider/modify-data.html


這節課將會向你展示如何使用一個 Intent 來插入一個新的或者修改一個現有的聯系人數據。與直接訪問Contacts Provider不同,一個 Intent 會啟動通訊錄應用所對應的 Activity 。對于這節課中所描述的修改操作, Intent 中將會包含啟動的 Activity 中你所輸入的那些數據。

使用 Intent 插入或者更新一個單一的聯系人是修改Contacts Provider最好的方法,原因如下:

它能夠節約你編寫自己的UI和代碼的時間

它可以避免修改時違反了Contacts Provider的規則而引入錯誤

它可以減少你需要申請的權限。你的應用不需要申請向Contacts Provider寫的權限,因為它將修改操作交給了通訊錄應用,而通訊錄應用已經有該權限了。


一). 使用一個Intent插入一個新的聯系人

你會經常希望當你的應用接收到新的數據后,允許用戶插入一個新的聯系人。例如,一個飯店查閱應用會允許用戶在查閱時以聯系人的形式添加餐館。使用intent來做這件事,使用你知道的盡可能多的數據來創建這個intent,之后將這個intent發送到通訊錄應用。

插入一個聯系人時,使用通訊錄應用將一個新的聯系人插入到Contacts Provider的 ContactsContract.RawContacts 表。必要的時候,通訊錄應用會在創建聯系人時提示用戶其賬戶類型和所使用的賬戶。當聯系人已存在時,通訊錄應用也會向用戶發出提示。之后用戶就可以選擇是否放棄插入,這樣的會就不會創建該聯系人。更多信息可以查看Contacts Provider的 API手冊

創建一個Intent

首先,創建一個新的 Intent 對象,它具有 Intents.Insert.ACTION 這一action。將MIME類型設置為 RawContacts.CONTENT_TYPE 。例如:

      
        ...


      
      
        //
      
      
         Creates a new Intent to insert a contact
      
      

Intent intent = 
      
        new
      
      
         Intent(Intents.Insert.ACTION);


      
      
        //
      
      
         Sets the MIME type to match the Contacts Provider
      
      

intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
    

如果你已經有了聯系人的詳細信息,比如一個電話號碼或者email地址,你可以將它們添加到intent中作為額外的數據。對于鍵而言,可以使用 Intents.Insert 中對應的常量。聯系人應用會在它的插入界面中顯示這些數據,讓用戶進一步編輯或者添加。

      
        /*
      
      
         Assumes EditText fields in your UI contain an email address

 * and a phone number.

 *

 
      
      
        */
      
      
        private
      
       EditText mEmailAddress =
      
         (EditText) findViewById(R.id.email);


      
      
        private
      
       EditText mPhoneNumber =
      
         (EditText) findViewById(R.id.phone);

...


      
      
        /*
      
      
        

 * Inserts new data into the Intent. This data is passed to the

 * contacts app's Insert screen

 
      
      
        */
      
      
        //
      
      
         Inserts an email address
      
      
        intent.putExtra(Intents.Insert.EMAIL, mEmailAddress.getText())


      
      
        /*
      
      
        

 * In this example, sets the email type to be a work email.

 * You can set other email types as necessary.

 
      
      
        */
      
      
        

      .putExtra(Intents.Insert.EMAIL_TYPE, CommonDataKinds.Email.TYPE_WORK)


      
      
        //
      
      
         Inserts a phone number
      
      
              .putExtra(Intents.Insert.PHONE, mPhoneNumber.getText())


      
      
        /*
      
      
        

 * In this example, sets the phone type to be a work phone.

 * You can set other phone types as necessary.

 
      
      
        */
      
      
        

      .putExtra(Intents.Insert.PHONE_TYPE, Phone.TYPE_WORK);
      
    

一旦你創建了 Intent ,通過調用 startActivity() 來發送它。

      
        /*
      
      
         Sends the Intent

     
      
      
        */
      
      
        

    startActivity(intent);
      
    

這一調用會在通訊錄應用中打開一個頁面,允許用戶添加一個新的聯系人。聯系人類型和名字會在頂部顯示。一旦用戶輸入了數據并且點擊了“ 完成 ”,會顯示通訊錄應用的聯系人列表,如果用戶點擊“ 返回 ”則會返回到原來的應用中。


二). 使用一個Intent編輯一個已存在的聯系人

如果用戶已經選擇了一個需要修改的聯系人,使用一個 Intent 編輯一個已存在的聯系人是很有用的。例如,一個應用發現聯系人有地址但是沒有郵編,那么用戶就可以為他添加郵編。

要使用Intent編輯一個已經存在的聯系人,使用的步驟和添加一個聯系人類似。向上一節那樣創建一個intent,然后對它添加聯系人的 Contacts.CONTENT_LOOKUP_URI 和MIME類型 Contacts.CONTENT_ITEM_TYPE 。如果你想要用你已經有的信息編輯聯系人,那么就把要修改的數據插入到intent中。注意,使用intent時,有些數據是不能修改的。這些數據可以在 API文檔 中“Update”這一標題下找到。

最后,發送這個intent。作為響應,聯系人應用會顯示一個編輯頁面。當用戶完成編輯并保存后,聯系人應用會顯示一個通訊錄列表。 如果用戶點擊“ 返回 ”則會返回到原來的應用中。

創建Intent

要編輯一個聯系人,調用 Intent(action) 來創建一個具有 ACTION_EDIT 這一action的intent。調用 setDataAndType() 來設置intent中 Contacts.CONTENT_LOOKUP_URI Contacts.CONTENT_ITEM_TYPE 的值;因為調用 setType() 會覆蓋掉當前的數據,所以你必須在同一時間設置數據和MIME類型。

要獲取一個聯系人的 Contacts.CONTENT_LOOKUP_URI ,調用 Contacts.getLookupUri(id, lookupkey) 方法,其中參數id是聯系人的 Contacts._ID 的值,參數lookupkey是 Contacts.LOOKUP_KEY 的值。

下面的代碼片段展示了如何創建這一intent:

      
        //
      
      
         The Cursor that contains the Contact row
      
      
        public
      
      
         Cursor mCursor;

    
      
      
        //
      
      
         The index of the lookup key column in the cursor
      
      
        public
      
      
        int
      
      
         mLookupKeyIndex;

    
      
      
        //
      
      
         The index of the contact's _ID value
      
      
        public
      
      
        int
      
      
         mIdIndex;

    
      
      
        //
      
      
         The lookup key from the Cursor
      
      
        public
      
      
         String mCurrentLookupKey;

    
      
      
        //
      
      
         The _ID value from the Cursor
      
      
        public
      
      
        long
      
      
         mCurrentId;

    
      
      
        //
      
      
         A content URI pointing to the contact
      
      
            Uri mSelectedContactUri;

    ...

    
      
      
        /*
      
      
        

     * Once the user has selected a contact to edit,

     * this gets the contact's lookup key and _ID values from the

     * cursor and creates the necessary URI.

     
      
      
        */
      
      
        //
      
      
         Gets the lookup key column index
      
      

    mLookupKeyIndex =
      
         mCursor.getColumnIndex(Contacts.LOOKUP_KEY);

    
      
      
        //
      
      
         Gets the lookup key value
      
      

    mCurrentLookupKey =
      
         mCursor.getString(mLookupKeyIndex);

    
      
      
        //
      
      
         Gets the _ID column index
      
      

    mIdIndex =
      
         mCursor.getColumnIndex(Contacts._ID);

    mCurrentId 
      
      =
      
         mCursor.getLong(mIdIndex);

    mSelectedContactUri 
      
      =
      
        

            Contacts.getLookupUri(mCurrentId, mCurrentLookupKey);

    ...

    
      
      
        //
      
      
         Creates a new Intent to edit a contact
      
      

    Intent editIntent = 
      
        new
      
      
         Intent(Intent.ACTION_EDIT);

    
      
      
        /*
      
      
        

     * Sets the contact URI to edit, and the data type that the

     * Intent must match

     
      
      
        */
      
      
        

    editIntent.setDataAndType(mSelectedContactUri,Contacts.CONTENT_ITEM_TYPE);
      
    

添加導航標識

在Android 4.0(API 版本14)及后續版本中,在通訊錄應用中的一個問題會導致錯誤地導航。當你的應用發送了一個編輯intent至通訊錄應用后,用戶編輯并保存了一個聯系人,當他們點擊返回后,會看到聯系人列表。要導航到你的應用中,他們必須點擊最近使用的應用清單然后再點擊你的應用。

要在Android 4.0.3(API版本15)及后續版本中解決這一問題,需要再intent中添加鍵:“ finishActivityOnSaveCompleted ”,其值是:“ true ”。早于Android 4.0版本的系統接受這一參數,但是它不會起到任何效果。具體做法如下所示:

      
        //
      
      
         Sets the special extended data for navigation
      
      

    editIntent.putExtra("finishActivityOnSaveCompleted", 
      
        true
      
      );
    

添加其他數據

要在 Intent 中添加其他額外的數據,調用 putExtra() 。你可以使用 Intents.Insert 中所定義的的鍵值來添加數據。記住 ContactsContract.Contacts 表中的一些列是不能修改的。這些列 可以在 API文檔 中“Update”這一標題下找到。

發送Intent

最后發送你構造的Intent。例如:

      
        //
      
      
         Sends the Intent
      
      

    startActivity(editIntent);
    

三). 使用Intent讓用戶選擇是要插入還是編輯

通過發送一個帶有 ACTION_INSERT_OR_EDIT 這一action的intent,你可以讓用戶選擇時要插入還是編輯一個聯系人。例如:一個電子郵件客戶端可以允許用戶添加一個接受到的電子郵件地址到一個新的聯系人中,或者將它添加到一個已存在的聯系人中。將此類型intent的MIME類型設置為 Contacts.CONTENT_ITEM_TYPE ,但不要設置數據URI。

當你發送了這一intent,通訊錄應用會顯示一個聯系人清單。用戶既可以插入一個新的聯系人,也可以選擇一個現有的聯系人并編輯它。任何你添加到intent中的數據會顯示出來。你可以使用 Intents.Insert 中定義的鍵值。下面的代碼片段展示了如何構造并發送該intent:

      
        //
      
      
         Creates a new Intent to insert or edit a contact
      
      

    Intent intentInsertEdit = 
      
        new
      
      
         Intent(Intent.ACTION_INSERT_OR_EDIT);

    
      
      
        //
      
      
         Sets the MIME type
      
      
            intentInsertEdit.setType(Contacts.CONTENT_ITEM_TYPE);

    
      
      
        //
      
      
         Add code here to insert extended data, if desired
      
      
            ...

    
      
      
        //
      
      
         Sends the Intent with an request ID
      
      

    startActivity(intentInsertEdit);
    

【Android Developers Training】 100. 使用Intent修改聯系人數據


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 和硕县| 阳西县| 星子县| 恩施市| 达尔| 盐津县| 延安市| 闽侯县| 富阳市| 台前县| 宜章县| 清远市| 中超| 雷波县| 吉林市| 兴海县| 夏邑县| 渭南市| 东宁县| 施秉县| 荃湾区| 黎城县| 临邑县| 江口县| 呼玛县| 永嘉县| 高青县| 含山县| 连州市| 揭阳市| 德令哈市| 芜湖县| 密山市| 樟树市| 会泽县| 扶风县| 鄢陵县| 宣化县| 宜君县| 海城市| 沿河|