Android(OPhone) 学习笔记 - Home API 的使用

来源:互联网 发布:外贸原单女装淘宝店 编辑:程序博客网 时间:2024/06/06 17:09
这是一个略复杂的例子:Home API Demo。主要实现了三个功能:1.将飞信未读符号添加至快捷方式。 2.将快捷方式添加至桌面。 3.注册一个广播的receiver,可以获知实时消息。
一、整体结构
考虑到本程序较前几个例子复杂,略去基本相同的工程介绍,首先来看HomeAPIDemoActivity.java。


首先来看onCreate,它有两个函数,initUI()和registerReceiver()。对于界面内容较多的程序,我们可以在一个单独函数里面专门设置界面以及注册响应程序的内容。



二、界面设置
在设置界面时,首先setContentView,然后定义界面中出现的控件,最后编写触发控件时所做的响应。



三、添加符号
在加入和移除symbol功能中,都用到了changeUnreadMessageCount函数,只是参数0和1的区别。功能是当飞信之类的软件有消息来的时候,改变其未读消息的数量。

这里mUnreadMessageCount是个全局,初始化时private int mUnreadMessageCount = 0 。取0和它大的那一个。然后用了broadcastUnreadMessageCount,由于未读消息是全局的,所以不需参数传递。

我们下面来分析broadcastUnreadMessageCount函数,由于该函数较长,我们分成一个个部分来解释。

Intent countNumIntent = new Intent(HomeIntents.ACTION_SHORTCUT_SYMBOL);
countNumIntent.putExtra(HomeIntents.SHORTCUT_SYMBOL_INTENT, fetionIntent);

这里HomeIntents是OPhone里特有的一个类,oms.home.HomeIntents,作用是将一个符号添加成快捷方式,putExtra方法的功能是向飞信的intent添加扩展数据。

Intent不是一个新的概念,它对系统传递的各种消息进行了分类。在《HelloWorld工程简介》中,我们提到:receiver也是application的运行时子元素。receiver通过增加intent-fliter来标识它需要接受哪些intent。当收到intent后,receiver将根据不同的intent进行不同的处理。当一个Intent发出后,所有注册了该intent的receiver都将会收到,系统会根据receiver在系统中的注册次序顺序发送。当一个receiver处理完该Intent后,系统才会向下一个receiver发送。当一个receiver有多个未接收的intent时,将按照intent发送的次序顺序接收。

if (mUnreadMessageCount > 0) {
Bitmap bg = BitmapFactory.decodeResource(getResources(),R.drawable.shortcut_msgcount);
Bitmap symbol = Bitmap.createBitmap(bg.getWidth(), bg.getHeight(),bg.getConfig());

创建好intent之后,当消息数量大于0时,建立两个bitmap类型作为图标, BitmapFactory.decodeResource 的返回值是一个bitmap,将一个资源的ID号转化成图片。然后新建一个symbol类型的bitmap。


这段代码比较简单,在屏幕上画了图标,并将消息条数也显示在图标的下面。设置完图标后,执行 
countNumIntent.putExtra(HomeIntents.SHORTCUT_SYMBOL_BITMAP, symbol)
向图片添加扩展数据,HomeIntents.SHORTCUT_SYMBOL_BITMAP实际上就是字符串"symbol_bitmap"。

最后,不管消息条数是否大于0,执行

countNumIntent.putExtra(HomeIntents.SHORTCUT_SYMBOL_POSITION,HomeIntents.SYM_TOP);
sendBroadcast(countNumIntent);

第一条语句设置位置数据,SHORTCUT_SYMBOL_POSITION有SYM_CENTER, SYM_TOP两种,第二条语句发送广播消息。该广播消息可以被所有感兴趣的receiver所接收,是异步的。

至此,符号添加/移除工作已经完成。运行结果如图:
加入symbol前:


加入symbol后:


四、添加快捷方式


本段代码中,实现了设置intent的类型为安装item,然后添加各种信息至intent,并且根据飞信图标创建一个快捷方式图标资源。最后将该内容发至广播。
快捷方式建立结果如下:



五、注册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<ResolveInfo> 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<ResolveInfo>的形式,这是指该函数可以加标志位,该标志位可以有
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的原理产生很多疑问,待后文分解。



原创粉丝点击