APP Widget là thành phần quan trọng trong Android, thường được sử dụng để hiển thị thông tin nhanh như đồng hồ, dự báo thời tiết...
Để thêm APP Widget lên màn hình chính, người dùng thực hiện thao tác nhấn giữ màn hình > chọn "Widget" > chọn ứng dụng cần thêm. Dưới đây sẽ hướng dẫn chi tiết cách tạo một APP Widget hiển thị thời gian thực thời.
Phát triển APP Widget cần 3 tệp XML sau:
(1) Tệp AndroidManifest.xml với cấu hình đặc thù cho widget:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vd.widgetgio"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".ThoiGianWidget" android:label="Đồng hồ">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/thoigian_widget" />
</receiver>
<service android:name=".DichVuCapNhat" />
</application>
</manifest>
Mỗi receiver trong manifest đại diện cho một APP Widget. Tham số android:name xác định lớp xử lý widget, android:label là tên hiển thị. Cấu hình liên kết với tệp cấu hình widget.
(2) Tệp cấu hình widget (res/xml/thoigian_widget.xml):
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="220dip"
android:minHeight="146dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_thoigian"
/>
Cấu hình kích thước tối thiểu (theo công thức 74*n-2), chu kỳ cập nhật (đặt 0 khi sử dụng timer), và layout hiển thị.
(3) Layout widget (res/layout/widget_thoigian.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtThoiGian"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Đang khởi động..."
android:textColor="#FFFF00"
android:textSize="20sp" />
Giống các layout thông thường nhưng chỉ chứa 1 TextView để hiển thị thời gian.
Lớp Java chính (ThoiGianWidget.java):
package com.vd.widgetgio;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
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.widget.RemoteViews;
public class ThoiGianWidget extends AppWidgetProvider {
private Timer timer;
private Context mContext;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
mContext = context;
timer = new Timer();
timer.schedule(capNhatThoiGian, 0, 1000);
}
public static class DichVuCapNhat extends Service {
@Override
public void onStart(Intent intent, int startId) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_thoigian);
views.setTextViewText(R.id.txtThoiGian, new Date().toLocaleString());
ComponentName widget = new ComponentName(this, ThoiGianWidget.class);
AppWidgetManager.getInstance(this).updateAppWidget(widget, views);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
};
private TimerTask capNhatThoiGian = new TimerTask() {
public void run() {
Intent dichVu = new Intent(mContext, DichVuCapNhat.class);
mContext.startService(dichVu);
}
};
}
Chương trình sử dụng Timer kích hoạt dịch vụ cập nhật mỗi giây. Dịch vụ lấy thời gian hiện tại và cập nhật lên widget thông qua AppWidgetManager.