Android有个非常好的功能,就是Widget控件,Widget作为应用的快捷方式和缩略展示,它在桌面上可以展示用户感兴趣的应用程序提供的信息,并且可以点击widget上的控件从而进入到应用的Activity。那么怎么去开发一个Widget呢,可以说是非常的人性化和方便的。下面是我在学习中创建的一个小实例。
参照Google的文档,我们首先得在清单文件中declare一个widget,代码如下。
<receiver android:name="ExampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver>
下面我们要去写一个类ExampleAppWidgetProvider它要继承AppWidgetProvider,还有我们注意这行:android:resource="@xml/example_appwidget_info,example_appwidget_info.xml是用来描述控件的元数据。下面分别给出相应的代码。
example_appwidget_info.xml
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="0" android:initialLayout="@layout/example_appwidget" > </appwidget-provider>
ExampleAppWidgetProvider.java
package cn.itcast.widget;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i("i", "onUpdate()");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = format.format(date);
views.setTextViewText(R.id.tv_time, time);
appWidgetManager.updateAppWidget(appWidgetIds[0], views);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Log.i("i", "onDeleted()");
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.i("i", "onEnabled()");
// 启动服务
Intent intent = new Intent(context, UpdateTimeService.class);
context.startService(intent);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.i("i", "onDisabled()");
// 停止服务
Intent intent = new Intent(context, UpdateTimeService.class);
context.stopService(intent);
}
}
如下XML文件是widget的布局文件,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff">
<TextView android:id="@+id/tv_time"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="我是widget"
android:textColor="#f00"/>
</LinearLayout>
下面这个类是一个service,它负责实时更新桌面上widget显示的时间,具体代码如下,
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
public class UpdateTimeService extends Service {
private Timer timer;
private TimerTask task = new TimerTask() {
@Override
public void run() {
// 得到widget管理器
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName componentName = new ComponentName(getApplicationContext(), ExampleAppWidgetProvider.class);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget);
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = formate.format(date);
views.setTextViewText(R.id.tv_time, time);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);
// 设置widget的点击事件
views.setOnClickPendingIntent(R.id.tv_time, pendingIntent);
// 更新widget
appWidgetManager.updateAppWidget(componentName, views);
}
};
@Override
public void onCreate() {
super.onCreate();
timer = new Timer();
timer.schedule(task, 0, 1000);// 开始任务
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();// 结束任务
}
}
清单文件中注册service,加上:
<service android:name=".UpdateTimeService"></service>