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长按添加这种傻傻的操作,而是将该功能放在窗口小部件中,但其实现的原理还是快捷方式,换汤不换药,但是这种方式用户更容易用到,提高了用户的可交换性。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章