Android桌面组件App Widget完整案例
程序员文章站
2023-12-01 19:12:34
本文实例讲述了android桌面组件app widget用法。分享给大家供大家参考。具体如下:
这里模拟一个案例:把appwidget添加到桌面后,点击appwidget...
本文实例讲述了android桌面组件app widget用法。分享给大家供大家参考。具体如下:
这里模拟一个案例:把appwidget添加到桌面后,点击appwidget后appwidget文本会轮回改变
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="程序入口" android:textsize="50dip"/> </linearlayout>
res/xml/my_appwidget.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minwidth="120dp" android:minheight="60dp" android:updateperiodmillis="1000" android:initiallayout="@layout/main"> </appwidget-provider>
清单文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.activity" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".testactivity"> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_appwidget"> </meta-data> <intent-filter> <action android:name="com.ljq.action.widget.click"></action> <action android:name="android.appwidget.action.appwidget_update" /> </intent-filter> </receiver> </application> <uses-sdk android:minsdkversion="7" /> </manifest>
变量类utiltool:用来控件文本改变:
package com.ljq.activity; public class utiltool { public static boolean ischange=true; }
testactivity类,继承自appwidgetprovider:
package com.ljq.activity; import android.app.pendingintent; 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 testactivity extends appwidgetprovider { // 自定义一个action名 private static final string action_click_name = "com.ljq.action.widget.click"; private remoteviews rv; @override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { system.out.println("onupdate"); //获取r.layout.main布局,通过类remoteviews对布局r.layout.main里的控件进行操作 /*rv = new remoteviews(context.getpackagename(), r.layout.main); intent intentclick = new intent(action_click_name); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, intentclick, 0); rv.setonclickpendingintent(r.id.tv, pendingintent); componentname cmp = new componentname(context, testactivity.class); appwidgetmanager myappwidgetmanager = appwidgetmanager.getinstance(context); myappwidgetmanager.updateappwidget(cmp, rv);*/ final int n = appwidgetids.length; for (int i = 0; i < n; i++) { int appwidgetid = appwidgetids[i]; updateappwidget(context, appwidgetmanager, appwidgetid); } } //appwidget生命周期: 每接收一次,广播执行一次为一个生命周期结束。 //也就是说在重写appwidgetprovider类里面声明全局变量做状态判断, //每次状态改变appwidgetprovider再接收第二次广播时即为你重新初始化也就是说重新实例化了一次appwidgetprovider。 //今天我因为在里面放了一个boolean值初始化为true,观察调试看到每次进入都为true故你在设置桌面组件时, //全局变量把它声明在另外一个实体类用来判断是没问题的,切忌放在本类。 @override public void onreceive(context context, intent intent) { system.out.println("onreceive"); if (rv == null) { rv = new remoteviews(context.getpackagename(), r.layout.main); } if (intent.getaction().equals(action_click_name)) { if (utiltool.ischange) { rv.settextviewtext(r.id.tv, "abc"); } else { rv.settextviewtext(r.id.tv, "123"); } utiltool.ischange = !utiltool.ischange; appwidgetmanager appwidgetmanger = appwidgetmanager.getinstance(context); int[] appids = appwidgetmanger.getappwidgetids(new componentname(context, testactivity.class)); appwidgetmanger.updateappwidget(appids, rv); }else{ super.onreceive(context, intent); } } private void updateappwidget(context context, appwidgetmanager appwidgemanger, int appwidgetid) { rv = new remoteviews(context.getpackagename(), r.layout.main); intent intentclick = new intent(); intentclick.setaction(action_click_name); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, intentclick, 0); rv.setonclickpendingintent(r.id.tv, pendingintent); appwidgemanger.updateappwidget(appwidgetid, rv); } }
希望本文所述对大家的android程序设计有所帮助。
上一篇: 教育类app该如何推广?