Direct Message功能簡單分析

Direct Message功能簡單分析

簡介

   Direct Message實現了快速向聯繫人發送短信的功能,如果按照正常的流程發送短信,首先進入短信應用程序,然後編輯會話,這時如果會話不存在我們還需要去查找聯繫人然後編輯短信發送,這樣的操作對於追求簡單快捷的我們來說太“複雜”。Direct Message它提供給用戶一個神馬樣的功能呢?

功能使用說明

  對於某些我們需要快速響應的聯繫人,添加Direct Message功能,怎麼添加?在android2.3中長按HomeScreen界面,在彈出的對話框中選擇快捷方式,然後在快捷方式中選擇Direct message,這時會彈出一個聯繫人界面,選擇聯繫人,選擇好後你就可以看到在桌面生成了該聯繫人的快捷方式,點擊該快捷方式就可以編輯或查看該聯繫人的會話。如下圖所示:
   

原理分析

  從使用來看,Direct Message是基於快捷方式來實現的,大家知道快捷方式的實現原理中,對應的應用程序必須對快捷方式的name、icon、以及點擊後的intent進行設置。Direct Message涉及到到兩個應用,一個是通訊錄,我們選擇的聯繫人是從通訊錄的ContactsListActivity界面;另一個是短信,當我們點擊快捷方式後跳轉到短息的編輯界面(ComposeMessageActivity)。很顯然Direct Message定義快捷方式的icon、name和intent是在通訊錄中。
  首先來看ContactsListActivity在AndroidManifest.xml中關於DirectMessage快捷方式的定義

      <activity-alias android:name="alias.MessageShortcut"
           android:targetActivity="ContactsListActivity"
           android:label="@string/shortcutMessageContact"
           android:icon="@drawable/ic_launcher_shortcut_directmessage">
           <intent-filter>
               <action android:name="android.intent.action.CREATE_SHORTCUT" />
               <category android:name="android.intent.category.DEFAULT" />
           </intent-filter>
       </activity-alias>

  然後再來看看ContactsListActivity中關於生成以後的快捷方式的icon和name以及點擊後的intent設置,在returnPickerResult方法中進行定義的下面是其核心設置的內容。

               // This is a direct dial or sms shortcut.
               mShortcutAction = Intent.ACTION_SENDTO;
               String number = c.getString(PHONE_NUMBER_COLUMN_INDEX);
               int type = c.getInt(PHONE_TYPE_COLUMN_INDEX);
               String scheme;
               int resid;
                   scheme = Constants.SCHEME_SMSTO;
                   resid = R.drawable.badge_action_sms;
               // Make the URI a direct tel: URI so that it will always continue to work
               Uri phoneUri = Uri.fromParts(scheme, number, null);
               shortcutIntent = new Intent(mShortcutAction, phoneUri);
               intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,
                       generatePhoneNumberIcon(selectedUri, type, resid));
           shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
           intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
           setResult(RESULT_OK, intent);

  最後當我們點擊快捷方式,怎麼連接到短信會話呢?
  從上面定義來看shortcutIntent定義時添加了一個Intent.ACTION_SENDTO的action,該action和數據與短息的編輯界面(ComposeMessageActivity)有什麼關係呢?下面請看ComposeMessageActivity的定義

        <activity android:name=".ui.ComposeMessageActivity"
                 android:configChanges="orientation|keyboardHidden"
                 android:windowSoftInputMode="stateHidden"
                 android:launchMode="singleTop" >
           <intent-filter>
               <action android:name="android.intent.action.VIEW" />
               <action android:name="android.intent.action.SENDTO" />
               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />
               <data android:scheme="sms" />
               <data android:scheme="smsto" />
           </intent-filter>

  看到這你就明白了爲什麼設置Intent.ACTION_SENDTO的action,在ComposeMessageActivity類中怎麼來判斷當前是從快捷方式點擊過來的了,是因爲在設置shortcutIntent時傳入了phoneUri,該uri中存入了號碼,ComposeMessageActivity就是根據該號碼來判斷的,如果該號碼的會話存在就會查詢出歷史記錄顯示,如果不存在則新建會話給用戶。

補充說明

  該小功能比較簡單,在android4.0以後取消了HomeScreen長按添加這種傻傻的操作,而是將該功能放在窗口小部件中,但其實現的原理還是快捷方式,換湯不換藥,但是這種方式用戶更容易用到,提高了用戶的可交換性。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章