简介
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就是根据该号码来判断的,如果该号码的会话存在就会查询出历史记录显示,如果不存在则新建会话给用户。
DirectMessage功能简化了向特定联系人发送短信的过程。通过在主屏幕上为联系人创建快捷方式,用户可以直接进入短信编辑界面,无需经过查找联系人等步骤。此功能在Android 2.3及后续版本中提供,并在Android 4.0后被整合进窗口小部件。
3665

被折叠的 条评论
为什么被折叠?



