Android Widget搭建过程是本文要介绍的内容,文中通过代码来实现Android Widget搭建,具体内容的实现来看本文详解。
Android平台下Widget的搭建过程为:
1、在res/layout下创建Widget的布局文件:digitalclock.xml
代码
- <?xml version="1.0" encoding="UTF-8"?>
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/time"
- android:textSize="45px"
- android:scrollX="30px"
- android:scrollY="30px"
- android:textStyle="bold"
- android:textColor="#ff000000"
- android:background="@drawable/bg"
- />
2、在res/xml下创建Widget的描述文件:est_appwidget.xml
代码
- <?xml version="1.0" encoding="UTF-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="186dip"
- android:minHeight="212dip"
- android:updatePeriodMillis="1000"
- android:initialLayout="@layout/digitalclock"/>
3、从AppWidgetProvider继承一个类(ESTTime),重写其虚方法
代码
- package com.android.test.esttime;
- import android.app.Service;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.os.IBinder;
- import android.text.format.Time;
- import android.widget.RemoteViews;
- public class ESTTime extends AppWidgetProvider {
- /** Called when the activity is first created. */
- @Override
- public void onUpdate(Context context,
- AppWidgetManager appWidgetManager,int[] appWidgetIds )
- {
- context.startService(new Intent(context,UpdateService.class));
- }
- //Service类
- public static class UpdateService extends Service {
- @Override
- public void onStart(Intent intent,int startId){
- Time estTime = new Time("EST");
- estTime.setToNow();
- RemoteViews updateViews =
- new RemoteViews(getPackageName(),
- R.layout.digitalclock);
- updateViews.setTextViewText(R.id.time, estTime.format("%H:%M"));
- ComponentName thisWidget = new ComponentName(this,ESTTime.class);
- AppWidgetManager manager = AppWidgetManager.getInstance(this);
- manager.updateAppWidget(thisWidget, updateViews);
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- }
- }
4、在AndroidManifest.xml中注册本Widget
代码
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.test.esttime"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <receiver android:name=".ESTTime"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/est_appwidget"/>
- </receiver>
- <service android:name=".ESTTime$UpdateService"/>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
最后,编译运行。这样在Home上长按弹出的Widget列表中就有新创建的Weiget了 。
小结:Android Widget搭建过程的内容介绍完了,希望通过Android Widget搭建内容的学习能对你有所帮助!