记录一下Android桌面小工具的实现方式
1.注册
首先需要在AndroidManifest.xml
中进行注册,类似Activity、Service的注册方式:
<receiver android:name=".MonitorWidgetProvider">
<meta-data
android:name="android.appwidget.provider"
android:resource="${APP_WIDGET}" />
<intent-filter>
<action android:name="com.medtrum.healthcareforandroid.app.monitorwidgetprovider.refresh" />
</intent-filter>
</receiver>
2.继承AppWidgetProvider
实现
public class MonitorWidgetProvider extends AppWidgetProvider{
public MonitorWidgetProvider() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
super.onRestored(context, oldWidgetIds, newWidgetIds);
}
}
从这些可以覆盖的方法名称就可以知道其调用的位置
3.配置文件
注意这里的${APP_WIDGET}
是对应的配置文件,我们需要在res/xml
中创建:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/view_monitor_sense"
android:previewImage="@mipmap/ic_launcher_sense"
android:minWidth="320dp"
android:minHeight="110dp"
android:updatePeriodMillis="60000"
android:resizeMode="horizontal|vertical"
android:minResizeWidth="250dp"
android:minResizeHeight="40dp"> </appwidget-provider>
解释一下这些参数的意思:
-
android:initialLayout
:对应的布局; -
android:previewImage
:桌面插件管理界面显示的icon; -
android:minWidth
:最小宽度,这个宽度计算为:70*n-30,n代表的时在桌面上占用n个格子; -
android:minHeight
:最小高度,同上; -
android:minResizeWidth
:最小可调节宽度,结合上方的最小宽度实现后结果即,插件第一次拖拽到桌面时候占用五个格子,长按调节宽度,最小可调节到四个格子宽; -
android:minResizeHeight
:最小可调节高度,同上; -
android:resizeMode
:可调节方向,回调方法为onAppWidgetOptionsChanged(...)
; -
android:updatePeriodMillis
:给定间隔更新,单位毫秒,回调方法为onUpdate(...)
;
4.主动更新Widget
我们在注册的时候就添加了action
:
<intent-filter>
<action android:name="com.medtrum.healthcareforandroid.app.monitorwidgetprovider.refresh" />
</intent-filter>
类似于发送静态广播,如下操作就可以主动更新Widget:
Intent intent = new Intent(MonitorWidgetProvider.APP_WIDGET_REFRESH);
sendBroadcast(intent);
注意,主动更新Widget中回调的方法为onReceive
;
5.RemoteViews
获取布局元素
在Widget
中需要使用RemoteViews
类来操作布局元素,这里和Notication
中元素的操作类似:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.view_monitor_all);
基本的元素操作就不再赘述,记录一下如何跳转到对应App指定界面事件:
Intent intent = new Intent(MyApplication.getInstance(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(MyApplication.getInstance(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.ll_widget_all, pendingIntent);
以上就是实现一个Widget需要记录的了,ok~
网友评论