詳解Android Widget創(chuàng)建過程
Android Widget創(chuàng)建過程是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Android Widget應(yīng)用的學(xué)習(xí),本文通過代碼很詳細的講解Android Widget創(chuàng)建過程。具體內(nèi)容的實現(xiàn)來看本文詳解。
創(chuàng)建一個Widget:
1、創(chuàng)建一個AppWidgetProvider類,設(shè)置提供的服務(wù):
- publicclasswidgetextendsAppWidgetProvider{
- @Override
- publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,int[]appWidgetIds){
- context.startService(newIntent(context,UpdateService.class));//啟動一個服務(wù)
- }
2、再創(chuàng)建一個Service類:
- publicstaticclassUpdateServiceextendsService{
- @Override
- publicvoidonStart(Intentintent,intstartId){
- TimeestTime=newTime("BEIJING");
- //TimewestTime=newTime("WEST");
- //westTime.setToNow();
- Stringtime1=estTime.getCurrentTimezone();//獲取手機當(dāng)前所在地的時區(qū)
- Timetime=newTime(time1);
- time.setToNow();
- //estTime.setToNow();
- intweek=time.getWeekNumber();//獲取當(dāng)天是本年的第幾周
- RemoteViewsupdateViews=newRemoteViews(getPackageName(),R.layout.digitalclock);//設(shè)置時鐘顯示的界面,此界面的設(shè)置如3所示
- //updateViews.setTextViewText(R.id.time,estTime.format("%H:%M:%S"));
- updateViews.setTextViewText(R.id.time,time.format("%H:%M:%S"));
- updateViews.setTextViewText(R.id.time1,"第"+week+"周");
- ComponentNamethisWidget=newComponentName(this,widget.class);
- AppWidgetManagerAppWidgetManagermanager=AppWidgetManager.getInstance(this);
- manager.updateAppWidget(thisWidget,updateViews);
- }
- @Override
- publicIBinderonBind(Intentintent){
- returnnull;
- }
3、設(shè)置時鐘顯示界面:
此文件為layout中的digitalclock.xml文件,
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:gravity="top"
- android:layout_width="146dip"
- android:layout_height="146dip"
- >
- android:paddingTop="10dip"
- android:id="@+id/time"
- android:textStyle="bold"
- android:textColor="#ff0000"
- android:gravity="center_horizontal"
- android:layout_width="146dip"
- android:layout_height="wrap_content"
- />
- android:id="@+id/time1"
- android:textColor="#ff0000"
- android:textStyle="bold"
- android:gravity="center_horizontal"
- android:layout_width="146dip"
- android:layout_height="20dip"
- />
- android:src="@drawable/mm"
- android:layout_width="146dip"
- android:layout_height="72dip"
- />
4、設(shè)置服務(wù):
此文件為res文件夾下xml文件夾中的est_appwidget.xml文件:
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="146dip"
- android:minHeight="146dip"
- android:updatePeriodMillis="1000"
- android:initialLayout="@layout/digitalclock"/>
5、Manifest.xml文件設(shè)置:
- package="com.test.widget"
- android:versionCode="1"
- android:versionName="1.0">
- android:label="@string/app_name">
- android:name="android.appwidget.action.APPWIDGET_UPDATE">
- android:name="android.appwidget.provider"
- android:resource="@xml/est_appwidget">
- android:name=".widget$UpdateService">
- android:label="@string/app_name">
6、第1和第2步合并起來就是一個Widget.java文件,如下所示:
- importandroid.app.Service;
- importandroid.appwidget.AppWidgetManager;
- importandroid.appwidget.AppWidgetProvider;
- importandroid.content.ComponentName;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.IBinder;
- importandroid.text.format.Time;
- importandroid.widget.RemoteViews;
- publicclasswidgetextendsAppWidgetProvider{
- @Override
- publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,int[]appWidgetIds){
- context.startService(newIntent(context,UpdateService.class));//啟動一個服務(wù)
- }
- publicstaticclassUpdateServiceextendsService{
- @Override
- publicvoidonStart(Intentintent,intstartId){
- TimeestTime=newTime("BEIJING");
- //TimewestTime=newTime("WEST");
- //westTime.setToNow();
- Stringtime1=estTime.getCurrentTimezone();
- Timetime=newTime(time1);
- time.setToNow();
- //estTime.setToNow();
- intweek=time.getWeekNumber();
- RemoteViewsupdateViews=newRemoteViews(getPackageName(),R.layout.digitalclock);
- //updateViews.setTextViewText(R.id.time,estTime.format("%H:%M:%S"));
- updateViews.setTextViewText(R.id.time,time.format("%H:%M:%S"));
- updateViews.setTextViewText(R.id.time1,"第"+week+"周");
- ComponentNamethisWidget=newComponentName(this,widget.class);
- AppWidgetManagerAppWidgetManagermanager=AppWidgetManager.getInstance(this);
- manager.updateAppWidget(thisWidget,updateViews);
- }
- @Override
- publicIBinderonBind(Intentintent){
- returnnull;
- }
- }
小結(jié):詳解Android Widget創(chuàng)建過程的內(nèi)容介紹完了,希望通過Android Widget創(chuàng)建內(nèi)容的學(xué)習(xí)能對你有所幫助。