簡介
Direct Message實現了快速向聯繫人發送短信的功能,如果按照正常的流程發送短信,首先進入短信應用程序,然後編輯會話,這時如果會話不存在我們還需要去查找聯繫人然後編輯短信發送,這樣的操作對於追求簡單快捷的我們來說太“複雜”。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就是根據該號碼來判斷的,如果該號碼的會話存在就會查詢出歷史記錄顯示,如果不存在則新建會話給用戶。