講解Android中的Widget及AppWidget小工具的創(chuàng)建實(shí)例
1.Widget 、App Widget 、Web App 的概念
Widget最初的概念是98年一個(gè)叫Rose的蘋果工程師提出,直到2003年的時(shí)候才正式為大家所知,不過隨后無數(shù)大公司都開始接受并應(yīng)用這一思路。 現(xiàn)在我們看到在蘋果系統(tǒng)里按下F4彈出的Dashboard里的小工具叫Widget,在Windows 7里側(cè)邊欄上的那些漂亮的小工具叫Gadget(widget變體?),除此以外還有yahoo Widget等等Widget產(chǎn)品。他們有一個(gè)共同的特點(diǎn)就是采用前臺(tái)Web開發(fā)用的技術(shù)(譬如HTML、CSS、Javascript)來制作的小工 具、小部件。
在Android系統(tǒng)里,幾乎每個(gè)可視化的View組件都叫Widget,起這個(gè)名字可能當(dāng)時(shí)是為了趕時(shí)髦。
App Widget是從Android 1.5以后才有的東東,就是可以放在Android桌面上的應(yīng)用程序小組件。這一點(diǎn)上看他的功能很像windows的側(cè)邊欄小工具,可惜的是他的采用技術(shù) 并不是HTML等技術(shù)。當(dāng)然App Widget才是我們本講的主角,本來他應(yīng)該順理成章叫做Widget的,至少也要叫做Gadget吧,可惜這個(gè)名字已經(jīng)被他自己的系統(tǒng)占用了,所以只好 改名叫App Widget。
最后講一下Web App 或者說是Android Web Application,也許叫mobile web application 更準(zhǔn)確些。我們發(fā)現(xiàn)現(xiàn)在智能機(jī)系統(tǒng)平臺(tái)很多,譬如iOS、Android、Windows Phone 、WebOS、BlackBerry等等,它們采用的技術(shù)框架也各不相同,有沒有辦法寫一個(gè)程序在各個(gè)系統(tǒng)上都能運(yùn)行呢?答案是肯定的,寫基于 Webkit的瀏覽器的應(yīng)用即可。我們使用 HTML5、CSS3、JavaScript、WebKit 等技術(shù)來寫的Web Application也許是今后的一個(gè)大潮流也說不準(zhǔn)啊。有機(jī)會(huì)我們?cè)僦v講Android Web Application 的開發(fā)。
2.創(chuàng)建一個(gè)最簡單的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為那個(gè)class(業(yè)務(wù)處理) --> <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是用來描述一個(gè)跨進(jìn)程顯示的view,也就是說這個(gè)view是在另外一個(gè)進(jìn)程顯示的。它inflate于layout資源文件。并且提供了可以修改過view內(nèi)容的一些簡單基礎(chǔ)的操作。
AppWidget---RemoteView,AppWidgetProvider是一個(gè)BrocaseReceiver,只是接受到Enable, Update,disale,delete這些message,而真正顯示界面的是AppWidgetHostView(這是在Launcher里面實(shí)現(xiàn)的),這中間就是通過RemoteView來溝通。通過RemoteView告訴Launcher你想要的AppWidget是長什么樣。
相關(guān)文章
Android實(shí)現(xiàn)滑動(dòng)選擇控件實(shí)例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)選擇控件實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
Android頂部工具欄和底部工具欄的簡單實(shí)現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡單實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05
logcat命令使用方法和查看android系統(tǒng)日志緩沖區(qū)內(nèi)容的方法
這篇文章主要介紹了logcat命令使用方法和查看android系統(tǒng)日志緩沖區(qū)內(nèi)容的方法,需要的朋友可以參考下2014-02-02
Android實(shí)現(xiàn)倒計(jì)時(shí)方法匯總
這篇文章主要為大家詳細(xì)總結(jié)了Android實(shí)現(xiàn)倒計(jì)時(shí)的3種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android?Flutter在點(diǎn)擊事件上添加動(dòng)畫效果實(shí)現(xiàn)全過程
這篇文章主要給大家介紹了關(guān)于Android?Flutter在點(diǎn)擊事件上添加動(dòng)畫效果實(shí)現(xiàn)的相關(guān)資料,通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息
這篇文章主要介紹了Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息的使用方法,代碼很簡單2014-01-01
Android開發(fā)adb.exe'' and can be executed.錯(cuò)誤解決方法
Android 實(shí)現(xiàn)夜間模式的快速簡單方法實(shí)例詳解

