widget使用与生命周期

第一步  创建一个没有界面的广播接收者  继承AppWidgetProvider

public class MyWidget extends AppWidgetProvider {

@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onreceive");
super.onReceive(context, intent);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
System.out.println("onupdate");
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
System.out.println("ondelete");
super.onDeleted(context, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
System.out.println("onenable");
super.onEnabled(context);
}

@Override
public void onDisabled(Context context) {
System.out.println("ondisable");
super.onDisabled(context);
}


}

第二步: 在清单文件中配置广播接收者

<receiver android:name="com.example.widget.MyWidget" >
<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>

第三步  在res/创建xml文件夹/创建example_appwidget_info.xml文件 内容如下 可参考文档

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="294dp"
android:updatePeriodMillis="1800000"
>
</appwidget-provider>

第四步  在res/layout目录下创建一个widget_layout.xml

<?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="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一个widget"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

生命周期

//1.当第一个widget被创建的时候
06-17 07:27:56.731: I/System.out(616): onreceive
06-17 07:27:56.731: I/System.out(616): onenable //第一次创建widget的时候会调用onenable 适合控件的初始化 开启后台服务定期获取数据 股票更新 客户端获取数据
06-17 07:27:56.741: I/System.out(616): onreceive
06-17 07:27:56.761: I/System.out(616): onupdate //1. 如果有新的widget创建 onupdate 会被调用 2. xml配置时间片到了 最短更新时间是半个小时


//2.当第二个创建的时候
06-17 07:29:30.652: I/System.out(616): onreceive
06-17 07:29:30.652: I/System.out(616): onupdate


//3.第三个创建
06-17 07:30:06.211: I/System.out(616): onreceive
06-17 07:30:06.211: I/System.out(616): onupdate


//....第N个创建
06-17 07:30:06.211: I/System.out(616): onreceive
06-17 07:30:06.211: I/System.out(616): onupdate

//当一个widget被移除
06-17 07:30:40.501: I/System.out(616): onreceive
06-17 07:30:40.501: I/System.out(616): ondelete

//最后一个widget被移除

06-17 07:31:19.972: I/System.out(616): onreceive
06-17 07:31:19.972: I/System.out(616): ondelete
06-17 07:31:19.981: I/System.out(616): onreceive
06-17 07:31:19.981: I/System.out(616): ondisable //最后一个widget被移除的时候 调用的方法. 清除扫尾操作. 擦屁股 停止服务 删除临时文件.


onenable //第一次创建widget的时候会调用onenable 适合控件的初始化 开启后台服务定期获取数据 股票更新 客户端获取数据
onupdate //1. 如果有新的widget创建 onupdate 会被调用 2. xml配置时间片到了 最短更新时间是半个小时
ondisable //最后一个widget被移除的时候 调用的方法. 清除扫尾操作. 擦屁股 停止服务 删除临时文件.
360桌面, 小米桌面, go桌面, htc sense 股票更新

原文地址:https://www.cnblogs.com/bravolove/p/5034655.html