Android开发之App widget用法实例分析
作者:hbiao68 时间:2021-09-18 16:25:54
本文实例讲述了Android开发之App widget用法。分享给大家供大家参考,具体如下:
放在桌面上的控件叫做——App widget,例如可以在桌面上添加按钮、图片等等控件,例如桌面播放器的控制面板
AppWidgetProviderInfo对象,它为App Widget提供元数据,包括布局、更新频率等等数据,这个对象不是由我们自己生成的,而是由android自己定义配置完成,这个对象被定义在XML文件中
1、定义AppWidgetProviderInfo对象,在res/xml文件夹当中定义一个名为widget_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="300dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_ui"
>
</appwidget-provider>
备注:建立的文件夹名一定是xml,因为只有这样才能被R识别
2、AppWidgetProvider定义了App Widget的基本生命周期
public class MyWidgetProvider extends AppWidgetProvider {
public static int Tag;
public int max;
public int current;
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
System.out.println("第一次被创建时调用这个方法");
}
@Override
public void onDisabled(Context context) {
System.out.println("当最后一个App Widget被删除时调用该方法");
}
@Override
public void onReceive(Context context, Intent intent) {
//调用父类的onReceive方法不能少,否则就无法监听到onUpdate事件了
super.onReceive(context, intent);
System.out.println("接收广播事件");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
System.out.println("在到达指定的更新时间之后或者当用户向桌面添加App Widget时调用这个方法");
for(int i = 0; i < appWidgetIds.length; i++){
Intent intent = new Intent(context, HB.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//R.layout.widget_ui指的是显示在桌面上的控件布局
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_ui);
//R.id.widgetButton指的是为桌面控件按钮绑定事件
remoteViews.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);
//updateAppWidget方法更新remoteViews
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds){
System.out.println("App Widget被删除时调用这个方法");
}
}
3、添加一个布局文件res/layout/widget_ui.xml(在桌面上显示的内容)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/widget_BT_Up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Value++"/>
<Button android:id="@+id/widget_BT_Down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value--"
android:layout_weight="1"/>
</LinearLayout>
4、在AndroidManifest.xml文件中添加reseiver标签
android:resource="@xml/widget_config" 指明显示widget_config.xml是appwidget的属性初始化设置
android:name="android.appwidget.action.APPWIDGET_UPDATE" 是android系统提供判定是appwidget的处理方式
android:name=".MyWidgetProvider" 表示处理的类,即继承了AppWidgetProvider类的类
<receiver android:name=".MyWidgetProvider" android:label="myWIdget" android:icon="@drawable/icon">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config"/>
</receiver>
备注:App Widget和我们应用程序运行在不同的进程中(App Widget当中的View运行在Home Screen进程中),因此要用到RemoteViews和PendingIntent这两个类来操控桌面的控件
如果你的onDelete、onUpdate等事件没有触发,那么一个重要的原因是,你override了onReceive事件,但是又没有调用super.onReceive(),所以导致这之后的事件都不会触发,AppWidgetProvider的事件处理机制是,onRecieve首先触发,然后由onReceive去触发后续事件。
希望本文所述对大家Android程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
flutter升级3.7.3报错Unable to find bundled Java version解决
![](https://img.aspxhome.com/file/2023/9/116149_0s.jpg)
Java8 Stream教程之collect()的技巧
![](https://img.aspxhome.com/file/2023/7/61507_0s.png)
升级dubbo2.7.4.1版本平滑迁移到注册中心nacos
JavaWeb开发之使用jQuery与Ajax实现动态联级菜单效果
![](https://img.aspxhome.com/file/2023/6/60466_0s.png)
IDEA的Swing可视化插件JFormDesigner详解
![](https://img.aspxhome.com/file/2023/6/62996_0s.jpg)
关于C#中的Invoke示例详解
基于Java实现收发电子邮件功能
![](https://img.aspxhome.com/file/2023/7/80307_0s.png)
C#实现把图片转换成二进制以及把二进制转换成图片的方法示例
C#获取所有进程的方法
![](https://img.aspxhome.com/file/2023/8/108808_0s.png)
手把手教你搭建SpringMVC框架——最小化配置
深入解析Android中的RecyclerView组件
![](https://img.aspxhome.com/file/2023/4/98654_0s.jpg)
SpringBoot配置文件方式,在线yml文件转properties
![](https://img.aspxhome.com/file/2023/7/99917_0s.png)
Java实现文件分割和文件合并实例
![](https://img.aspxhome.com/file/2023/5/79865_0s.png)
Unity 使用tiledmap解析地图的详细过程
![](https://img.aspxhome.com/file/2023/4/96314_0s.jpg)
C# 9.0新特性——只初始化设置器
Android实战APP启动速度优化
Spring Bean初始化及销毁多种实现方式
![](https://img.aspxhome.com/file/2023/0/71620_0s.jpg)
Java压缩/解压文件的实现代码
Android Git submodule详解用法示例
![](https://img.aspxhome.com/file/2023/5/103875_0s.png)