下面是我学习Widget的一些笔记:
一、特点:
1.快捷方便
2.个性化定制和功能
3.及时控制更新显示内容
二、步骤:
1.绘制Widget布局
2.配置基本属性
3.定义AppWidgetProvider
4.提供Service或其他,在provider调用
三、如何显示具体内容
1.创建一个RemoveViews对象,创建该对象时可以指定加载指定的界面布局文件
2.如果需要改变上一步所加载的界面布局文件的内容(主要包含ImageView和TextView两种组件),则可通过RemoteViews对象进行修改
3.创建一个ComponentName对象
4.调用AppWidgetManager更新桌面控件
example:
1.layout中创建widget.xml页面
加入一个显示时间的TextView
2.创建xml目录并创建widget的属性文件宽度,高度,布局,刷新时间
新建xml/widgetconfig.xml,选择appwidget-provider:
<?xml version="1.0" encoding="utf-8"?>
3.创建Provider类继承AppWidgetProvider重写onEnabled(context.startServices()),onDisabled(context.deleteServices()),onDeleted(),onUpdated()
<appwidget-provider xmlns:android="
http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="100dp"
android:updatePeriodMillis="864000" >
<!--
主要设置的参数如下:
minWidth: 定义Wdiget组件的宽度
minHeight: 定义Wdiget组件的高度
updatePeriodMillis: 更新的时间周期
initialLayout: Widget的布局文件 -->
minWidth: 定义Wdiget组件的宽度
minHeight: 定义Wdiget组件的高度
updatePeriodMillis: 更新的时间周期
initialLayout: Widget的布局文件 -->
</appwidget-provider>
public class WidgetProvider extends AppWidgetProvider {
4.在包下创建TimerService类继承Service类重写onCreate(),onDestroy():
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
// Widget从屏幕移除
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
// 最后一个Widget被从屏幕移除执行
context.stopService(new Intent(context, TimerSerivce.class));
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
// 第一个Widget添加到屏幕上执行
context.startService(new Intent(context, TimerSerivce.class));
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
// 刷新widget
// 通过remoteView和AppWidgetManager
}
}
public class TimerSerivce extends Service {
private Timer timer;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
updateViews();
}
}, 0, 1000);
}
private void updateViews() {
String time = sdf.format(new Date());
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget);
rv.setTextViewText(R.id.text, time);
AppWidgetManager manager = AppWidgetManager
.getInstance(getApplicationContext());
ComponentName cn = new ComponentName(getApplicationContext(), WidgetProvider.class);
manager.updateAppWidget(cn, rv);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer = null;
}
}
5.修改清单文件,添加权限
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.widgetdemo.WidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widgetconfig"/>
</receiver>
<service android:name="com.example.widgetdemo.TimerSerivce" >
</service>
</application>
附:
Widget并不支持所有的布局和控件,而仅仅只是支持Android布局和控件的一个子集。
(01) App Widget支持的布局:
FrameLayout
LinearLayout
RelativeLayout
GridLayout
(02) App Widget支持的控件:
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
(01) App Widget支持的布局:
FrameLayout
LinearLayout
RelativeLayout
GridLayout
(02) App Widget支持的控件:
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper