技术开发 频道

OPhone学习笔记 - Home API 的使用

  五、注册receiver

  在程序中首先定义了mHomeIntentReceiver这个变量,用于接收控件添加的消息并显示,这里是快捷方式添加成功的消息。

  private BroadcastReceiver mHomeIntentReceiver = new BroadcastReceiver() {

  @Override

  public void onReceive(Context context, Intent intent) {

  final String action = intent.getAction();

  if (HomeIntents.ACTION_ITEM_ADDED.equals(action)) {

  txtInfo.setText("Echo: Add shortcut sucess!");

  }

  }

  };

  在本文最前面,设置了界面的下一步工作是注册receiver,下面我们来看看如何注册。

  我们注册了两个filter,HomeIntents.ACTION_ITEM_ADDED和HomeIntents.ACTION_LOAD_COMPLETE,前者对一个控件添加时产生响应,后者对主屏幕装载完成是产生响应。最后,对filter进行注册。注册receiver时又使用了两个函数getFetionLauncherIntent和createShortcutIntent。

  private static Intent getFetionLauncherIntent(Context ctx, Intent src) {

  List mainActivitiesList = ctx.getPackageManager().queryIntentActivities(src, 0);

  if(mainActivitiesList != null && mainActivitiesList.size() > 0) {

  ResolveInfo resolveInfo = mainActivitiesList.get(0);

  Intent actIntent = new Intent(Intent.ACTION_MAIN);

  actIntent.addCategory(Intent.CATEGORY_LAUNCHER);

  actIntent.setComponent(new ComponentName(

  resolveInfo.activityInfo.applicationInfo.packageName,

  resolveInfo.activityInfo.name));

  return actIntent;

  }

  return null;

  }

  首先我们补充一些关于intent的理论知识,intent主要包含以下几个属性:

  action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

  data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

  category -- Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.

  type -- Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.

  component -- Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.

  extras -- This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.

  这样,我们就可以理解之前的extras的用途,实际上是保存附加信息的。queryIntentActivities的功能是在指定的intent中获取所有可能执行的activity列表,该函数通过获取飞信运行消息列表,放在intent中。

  大家会注意到List的形式,这是指该函数可以加标志位,该标志位可以有

  MATCH_DEFAULT_ONLY、GET_INTENT_FILTERS、GET_RESOLVED_FILTER三种形式。具体使用可以去查询http://developer.android.com/reference/android/content/pm/PackageManager.html。

  快捷方式的intent创建函数如下:

  private Intent createShortcutIntent() {

  Intent intent = new Intent(Intent.ACTION_MAIN);

  intent.setClassName(this, this.getClass().getName());

  intent.putExtra("oms.samples.home.HomeAPIDemo", "ApiDemos provided this shortcut");

  return intent;

  }

  六、结束

  至此,Home API的主要代码介绍到此结束,工程的其他部分如资源、AndroidManifest.xml未作介绍。学完本例子后,我们对intent、receiver的原理产生很多疑问,待后文分解。

0
相关文章