亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

【Android Developers Training】 100. 使用Int

系統(tǒng) 2402 0

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

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


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

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

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

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

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


一). 使用一個Intent插入一個新的聯(lián)系人

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

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

創(chuàng)建一個Intent

首先,創(chuàng)建一個新的 Intent 對象,它具有 Intents.Insert.ACTION 這一action。將MIME類型設(shè)置為 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);
    

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

      
        /*
      
      
         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);
      
    

一旦你創(chuàng)建了 Intent ,通過調(diào)用 startActivity() 來發(fā)送它。

      
        /*
      
      
         Sends the Intent

     
      
      
        */
      
      
        

    startActivity(intent);
      
    

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


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

如果用戶已經(jīng)選擇了一個需要修改的聯(lián)系人,使用一個 Intent 編輯一個已存在的聯(lián)系人是很有用的。例如,一個應(yīng)用發(fā)現(xiàn)聯(lián)系人有地址但是沒有郵編,那么用戶就可以為他添加郵編。

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

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

創(chuàng)建Intent

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

要獲取一個聯(lián)系人的 Contacts.CONTENT_LOOKUP_URI ,調(diào)用 Contacts.getLookupUri(id, lookupkey) 方法,其中參數(shù)id是聯(lián)系人的 Contacts._ID 的值,參數(shù)lookupkey是 Contacts.LOOKUP_KEY 的值。

下面的代碼片段展示了如何創(chuàng)建這一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);
      
    

添加導(dǎo)航標(biāo)識

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

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

      
        //
      
      
         Sets the special extended data for navigation
      
      

    editIntent.putExtra("finishActivityOnSaveCompleted", 
      
        true
      
      );
    

添加其他數(shù)據(jù)

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

發(fā)送Intent

最后發(fā)送你構(gòu)造的Intent。例如:

      
        //
      
      
         Sends the Intent
      
      

    startActivity(editIntent);
    

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

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

當(dāng)你發(fā)送了這一intent,通訊錄應(yīng)用會顯示一個聯(lián)系人清單。用戶既可以插入一個新的聯(lián)系人,也可以選擇一個現(xiàn)有的聯(lián)系人并編輯它。任何你添加到intent中的數(shù)據(jù)會顯示出來。你可以使用 Intents.Insert 中定義的鍵值。下面的代碼片段展示了如何構(gòu)造并發(fā)送該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修改聯(lián)系人數(shù)據(jù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 黄色片网站视频 | 越南黄色录像 | 午夜性a一级毛片 | 丁香激情综合色伊人久久 | 涩涩视频在线观看 | 久久综合久久鬼 | 四虎播播 | 麻豆国产高清精品国在线 | 欧美日韩国产人成在线观看 | 亚洲成片观看四虎永久 | 亚洲综合在线另类色区奇米 | 日日操免费视频 | 天天操夜夜操 | 久青草国产手机在线观 | 国产剧情一区二区三区 | 四虎影视久久久 | 久久 精品 一区二区 | 免费的一级毛片 | 99伊人| 免费观看成人久久网免费观看 | 开心激情四房 | 91精品久久久久亚洲国产 | 成人免费视频视频在线不卡 | 尹人综合网| 精品免费视频 | 日韩亚洲成a人片在线观看 日韩亚洲第一页 | 国产伦乱 | 亚洲精品国产福利一区二区三区 | 老司机永久免费网站在线观看 | 亚洲欧洲一区二区三区久久 | 亚洲欧美国产18 | 奇米色在线视频 | 深夜影院在线观看 | 性一级视频 | 99热.| 久久国产综合尤物免费观看 | 国产色啪午夜免费视频 | a毛片全部免费播放 | 亚洲经典在线中文字幕 | 日本一级毛片中文字幕 | 青青久久99久久99久久999 |