1.Widget 、App Widget 、Web App 的概念
Widget最初的概念是98年一个叫Rose的苹果工程师提出,直到2003年的时候才正式为大家所知,不过随后无数大公司都开始接受并应用这一思路。 现在我们看到在苹果系统里按下F4弹出的Dashboard里的小工具叫Widget,在Windows 7里侧边栏上的那些漂亮的小工具叫Gadget(widget变体?),除此以外还有yahoo Widget等等Widget产品。他们有一个共同的特点就是采用前台Web开发用的技术(譬如HTML、CSS、Javascript)来制作的小工 具、小部件。
在Android系统里,几乎每个可视化的View组件都叫Widget,起这个名字可能当时是为了赶时髦。
App Widget是从Android 1.5以后才有的东东,就是可以放在Android桌面上的应用程序小组件。这一点上看他的功能很像windows的侧边栏小工具,可惜的是他的采用技术 并不是HTML等技术。当然App Widget才是我们本讲的主角,本来他应该顺理成章叫做Widget的,至少也要叫做Gadget吧,可惜这个名字已经被他自己的系统占用了,所以只好 改名叫App Widget。
最后讲一下Web App 或者说是Android Web Application,也许叫mobile web application 更准确些。我们发现现在智能机系统平台很多,譬如iOS、Android、Windows Phone 、WebOS、BlackBerry等等,它们采用的技术框架也各不相同,有没有办法写一个程序在各个系统上都能运行呢?答案是肯定的,写基于 Webkit的浏览器的应用即可。我们使用 HTML5、CSS3、JavaScript、WebKit 等技术来写的Web Application也许是今后的一个大潮流也说不准啊。有机会我们再讲讲Android Web Application 的开发。
2.创建一个最简单的Widget
代码案例:
1)main.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:orientation=\"vertical\" android:layout_width=\"fill_parent\" android:layout_height=\"fill_parent\"> <TextView android:id=\"@+id/tvCurrTime\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"@string/hello\" android:textColor=\"@color/black\"/> </LinearLayout>
2)hello_widget_provider.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?> <appwidget-provider xmlns:android=\"http://schemas.android.com/apk/res/android\" android:minWidth=\"146dip\" android:minHeight=\"72dip\" android:initialLayout=\"@layout/main\"> </appwidget-provider>
3)AndroidManifest.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?> <manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.woody.testWidget\" android:versionCode=\"1\" android:versionName=\"1.0\"> <application android:icon=\"@drawable/icon\" android:label=\"@string/app_name\"> <receiver android:name=\".HelloWidgetProvider\" android:label=\"@string/app_name\"> <!-- HelloWidgetProvider为那个class(业务处理) --> <intent-filter> <action android:name=\"android.appwidget.action.APPWIDGET_UPDATE\" /> <!-- 指定了的 --> </intent-filter> <meta-data android:name=\"android.appwidget.provider\"android:resource=\"@xml/hello_widget_provider\" /> <!-- 为上面指定了的widget --> </receiver> </application> </manifest>
4)HelloWidgetProvider.java
public class HelloWidgetProvider extends AppWidgetProvider { /** Called when the activity is first created. */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000); } public class MyTime extends TimerTask { RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault()); public MyTime(Context context, AppWidgetManager appWidgetManager) { this.appWidgetManager = appWidgetManager; remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); thisWidget = new ComponentName(context, HelloWidgetProvider.class); } @Override public void run() { remoteViews.setTextViewText(R.id.tvCurrTime, \"Time = \" + format.format(new Date())); appWidgetManager.updateAppWidget(thisWidget, remoteViews); } } }
代码解释:RemoteView是用来描述一个跨进程显示的view,也就是说这个view是在另外一个进程显示的。它inflate于layout资源文件。并且提供了可以修改过view内容的一些简单基础的操作。
AppWidget---RemoteView,AppWidgetProvider是一个BrocaseReceiver,只是接受到Enable, Update,disale,delete这些message,而真正显示界面的是AppWidgetHostView(这是在Launcher里面实现的),这中间就是通过RemoteView来沟通。通过RemoteView告诉Launcher你想要的AppWidget是长什么样。
本文地址:https://www.stayed.cn/item/5079
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我