Android Widget开发代码示例详细说明

因为AppWidgetProvider扩展自BroadcastReceiver, 所以你不能保证回调函数完成调用后,AppWidgetProvider还在继续运行。

a. AppWidgetProvider 的实现

/*** Copyright(C):教育电子有限公司 * Project Name: NineSync* Filename: SynWidgetProvider.java * Author(S): Rjdeng* Created Date: 2013-4-23 下午8:55:42 * Version: V1.00* Description: 九科同步挂件*/package com.eebbk.synstudy.widget;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;import com.eebbk.synstudy.R;public class SynWidgetProvider extends AppWidgetProvider
{public static final String SUBJECT_MARK = "subject";private static final int CHINESE_CONSTANT = -1;private final String NINESYNC_PKG = "com.eebbk.synstudy";private final String NINESYNC_CLS = "com.eebbk.synstudy.welcome.WelcomActivity";private final String STUDYCARS_PKG = "com.eebbk.readingcard.readingcard";private final String STUDYCARS_CLS = "com.eebbk.readingcard.readingcard.MenuManagerActivity";private final int widgetViewId[] = { R.id.syn_widget_notes, R.id.syn_widget_chinese, R.id.syn_widget_math,R.id.syn_widget_english, R.id.syn_widget_physics, R.id.syn_widget_chemistry, R.id.syn_widget_organisms,R.id.syn_widget_history, R.id.syn_widget_geography, R.id.syn_widget_political };@Overridepublic void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ){// TODO Auto-generated method stubSystem.out.printf( "###九科同步挂件创建\n" );final RemoteViews views = new RemoteViews( context.getPackageName( ), R.layout.main_syn_widget );setViewOnClick( context, views );pushUpdate( context, appWidgetIds, views );super.onUpdate( context, appWidgetManager, appWidgetIds );}@Overridepublic void onDeleted( Context context, int[] appWidgetIds ){// TODO Auto-generated method stubSystem.out.printf( "###九科同步挂件删除\n" );super.onDeleted( context, appWidgetIds );}@Overridepublic void onEnabled( Context context ){// TODO Auto-generated method stubSystem.out.printf( "###当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用\n" );super.onEnabled( context );}private void setViewOnClick( Context context, RemoteViews views ){Intent intent;PendingIntent pendingIntent;ComponentName componentName;//九科同步componentName = new ComponentName( NINESYNC_PKG, NINESYNC_CLS );for ( int i = 1; i < widgetViewId.length; i++ ){intent = new Intent( );intent.setComponent( componentName );intent.putExtra( SUBJECT_MARK, CHINESE_CONSTANT + i );//for putExtraintent.setAction( String.valueOf( System.currentTimeMillis( ) ) );pendingIntent = PendingIntent.getActivity( context, 0 /* no requestCode */, intent, 0 /* no flags */);views.setOnClickPendingIntent( widgetViewId[i], pendingIntent );}//读书卡片intent = new Intent( );componentName = new ComponentName( STUDYCARS_PKG, STUDYCARS_CLS );intent.setComponent( componentName );pendingIntent = PendingIntent.getActivity( context, 0 /* no requestCode */, intent, 0 /* no flags */);views.setOnClickPendingIntent( widgetViewId[0], pendingIntent );}private void pushUpdate( Context context, int[] appWidgetIds, RemoteViews views ){// Update specific list of appWidgetIds if given, otherwise default to allfinal AppWidgetManager gm = AppWidgetManager.getInstance( context );if( appWidgetIds != null ){gm.updateAppWidget( appWidgetIds, views );}else{gm.updateAppWidget( new ComponentName( context, this.getClass( ) ), views );}}}

b. widget外观布局定义文件(文件位置:res\layout)

<?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"android:background="@drawable/syn_widget_bg"android:orientation="horizontal" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="20dip" ><Buttonandroid:id="@+id/syn_widget_notes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:background="@drawable/readnotes_selector" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="50dip"android:layout_marginTop="100dip"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1"android:orientation="horizontal" ><ImageButtonandroid:id="@+id/syn_widget_chinese"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/chinese_selector"android:clickable="true" /><ImageButtonandroid:id="@+id/syn_widget_math"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/math_selector" /><ImageButtonandroid:id="@+id/syn_widget_english"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/english_selector" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1"android:orientation="horizontal" ><ImageButtonandroid:id="@+id/syn_widget_physics"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/physics_selector" /><ImageButtonandroid:id="@+id/syn_widget_chemistry"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/chemistry_selector" /><ImageButtonandroid:id="@+id/syn_widget_organisms"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/organisms_selector" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1"android:orientation="horizontal" ><ImageButtonandroid:id="@+id/syn_widget_history"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/history_selector" /><ImageButtonandroid:id="@+id/syn_widget_geography"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/geography_selector" /><ImageButtonandroid:id="@+id/syn_widget_political"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/political_selector" /></LinearLayout></LinearLayout></LinearLayout>

c. 新增widget时的配置Activity的实现(可选)本介绍没有选择这种方式

d. widget 参数配置文件(文件位置:res\xml)

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:initialLayout="@layout/main_syn_widget"android:minHeight="505dip"android:minWidth="729dip"android:previewImage="@drawable/sync_launcher"android:updatePeriodMillis="0" ></appwidget-provider>

e. AndroidManifest.xml 声明

<receiverandroid:name=".widget.SynWidgetProvider"android:permission="android.permission.BIND_APPWIDGET" ><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/syn_widget_config" />
</receiver>

f. 效果图(九科同步挂件Rjdeng 于2013-4-24
在这里插入图片描述

觉得本文对你有用,麻烦点赞或关注或收藏,你的肯定是我创作的无限动力,谢谢!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/315781.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

逆向案例三十——webpack登录某游戏

网址&#xff1a;aHR0cHM6Ly93d3cuZ205OS5jb20v 步骤&#xff1a; 进行抓包分析&#xff0c;找到登录接口&#xff0c;发现密码有加密 跟栈分析&#xff0c;从第三个栈进入&#xff0c;打上断点&#xff0c;再次点击登录 明显找到password,它由o赋值&#xff0c;o由a.encode(…

gitee / github 配置git, 实现免密码登录

文章目录 怎么配置公钥和私钥验证配置成功问题 怎么配置公钥和私钥 以下内容参考自 github ssh 配置&#xff0c;gitee的配置也是一样的&#xff1b; 粘贴以下文本&#xff0c;将示例中使用的电子邮件替换为 GitHub 电子邮件地址。 ssh-keygen -t ed25519 -C "your_emai…

R语言的基本图形

一&#xff0c;条形图 安装包 install.packages("vcd") 绘制简单的条形图 barplot(c(1,2,4,5,6,3)) 水平条形图 barplot(c(1,2,4,5,6,3),horiz TRUE) 堆砌条形图 > d1<-c("Placebo","Treated") > d2<-c("None",&qu…

【Flink入门修炼】2-3 Flink Checkpoint 原理机制

如果让你来做一个有状态流式应用的故障恢复&#xff0c;你会如何来做呢&#xff1f; 单机和多机会遇到什么不同的问题&#xff1f; Flink Checkpoint 是做什么用的&#xff1f;原理是什么&#xff1f; 一、什么是 Checkpoint&#xff1f; Checkpoint 是对当前运行状态的完整记…

springboot 集成 activemq

文章目录 一&#xff1a;说明二&#xff1a;e-car项目配置1 引入activemq依赖2 application启动类配置消息监听3 application.yml配置4 MQConfig.java 配置类5 ecar 项目中的监听6 junit 发送消息 三&#xff1a;tcm-chatgpt项目配置5 MQListener.java 监听消息 三 测试启动act…

上位机图像处理和嵌入式模块部署(树莓派4b设置ftp下载)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 作为一个开发板&#xff0c;最好支持ftp下载&#xff0c;这样文件的上传和下载都会比较方便。虽然目前为止&#xff0c;利用mobaxterm和ssh也能实现…

Java小白福音丨保姆级的JDK+Eclipse+其他常用软件安装教程!

是的我看见到处是阳光 JDK正在安装 新世界来得像梦一样 让我暖洋洋 你的Java学习还在继续吗 你的JDK安装了吗 这儿有一份开发软件安装新教程 你不想学学吗 明天一早&#xff0c; 我猜阳光会好 我要把自己打扫 把破旧的套路丢掉 哦这样多好 加油吧Java少年 前言 想学习Java&…

使用大卫的k8s监控面板(k8s+prometheus+grafana)

问题 书接上回&#xff0c;对EKS&#xff08;AWS云k8s&#xff09;启用AMP&#xff08;AWS云Prometheus&#xff09;监控AMG(AWS云 grafana)&#xff0c;上次我们只是配通了EKSAMPAMG的监控路径。这次使用一位大卫老师的grafana的面板&#xff0c;具体地址如下&#xff1a; ht…

HarmonyOS hsp制作与引用

1. HarmonyOS hsp制作与引用 1.1 介绍 HSP动态共享包&#xff08;模块&#xff09;,应用内HSP指的是专门为某一应用开发的HSP&#xff0c;只能被该应用内部其他HAP/HSP使用&#xff0c;用于应用内部代码、资源的共享。应用内HSP跟随其宿主应用的APP包一起发布&#xff0c;与该…

day83 AJAX

一&#xff1a;什么是AJAX AJAX语法 AJAX Asynchronous JavaScript and XML 异步js和XML 实现页面某一部份更新&#xff0c;无需服务器转发或重定向 1 $.ajax() 语法: $.ajax( { "url" : "url", …

4.Docker本地镜像发布至阿里云仓库、私有仓库、DockerHub

文章目录 0、镜像的生成方法1、本地镜像发布到阿里云仓库2、本地镜像发布到私有仓库3、本地镜像发布到Docker Hub仓库 Docker仓库是集中存放镜像的地方&#xff0c;分为公共仓库和私有仓库。 注册服务器是存放仓库的具体服务器&#xff0c;一个注册服务器上可以有多个仓库&…

kubeadmin搭建自建k8s集群

一、安装要求 在开始之前&#xff0c;部署Kubernetes集群的虚拟机需要满足以下几个条件&#xff1a; 操作系统 CentOS7.x-86_x64硬件配置&#xff1a;2GB或更多RAM&#xff0c;2个CPU或更多CPU&#xff0c;硬盘30GB或更多【注意master需要两核】可以访问外网&#xff0c;需要…

Qt窗口

QMainWindow Qt 窗⼝ 是通过 QMainWindow类 来实现的。 QMainWindow 是⼀个为⽤⼾提供主窗⼝程序的类&#xff0c;继承⾃ QWidget 类&#xff0c;并且提供了⼀个预定义的 布局。QMainWindow 包含 ⼀个菜单栏&#xff08;menu bar&#xff09;、多个⼯具栏(tool bars)、多个浮动…

Python并发编程:揭开多线程与异步编程的神秘面纱

第一章&#xff1a;并发编程导论 1.1 并发与并行概念解析 1.1.1 并发性与并行性的区别 想象一下繁忙的厨房中多位厨师同时准备不同的菜肴——即使他们共享有限的空间和资源&#xff0c;也能协同工作&#xff0c;这就是并发性的一个生动比喻。并发性意味着多个任务在同一时间…

【哈希】Leetcode 面试题 01.02. 判定是否互为字符重排

题目讲解 面试题 01.02. 判定是否互为字符重排 算法讲解 直观的想法&#xff1a;我们找到一个字符串的全排列&#xff0c;然后对比当前的排列是否等于另一个字符串。如果两个字符串如果互为排列&#xff0c;所以我们知道两个字符串对应的字符出现的个数相同&#xff0c;那么…

常用图像加密技术-流密码异或加密

异或加密是最常用的一种加密方式&#xff0c;广泛的适用于图像处理领域。这种加密方式依据加密密钥生成伪随机序列与图像的像素值进行异或操作&#xff0c;使得原像素值发生变化&#xff0c;进而使得图像内容发生变化&#xff0c;达到保护图像内容的目的。 该加密方法是以图像…

Aiseesoft Blu-ray Player for Mac:蓝光播放器

Aiseesoft Blu-ray Player for Mac是一款功能强大且易于使用的蓝光播放器&#xff0c;专为Mac用户打造。它以其卓越的性能和简洁的操作界面&#xff0c;为用户带来了全新的高清蓝光播放体验。 Aiseesoft Blu-ray Player for Mac v6.6.50激活版下载 这款软件支持播放任何高质量的…

【Leetcode每日一题】 动态规划 - 简单多状态 dp 问题 - 打家劫舍 II(难度⭐⭐)(67)

1. 题目解析 题目链接&#xff1a;213. 打家劫舍 II 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 2.算法原理 这个问题是经典的“打家劫舍”问题的变种&#xff0c;原问题是在单排房屋中进行偷窃&#xff0c;而这个问题则是在…

机器学习:基于Sklearn、XGBoost框架,使用XGBClassifier、支持向量分类器和决策树分类器预测乳腺癌是良性还是恶性

前言 系列专栏&#xff1a;机器学习&#xff1a;高级应用与实践【项目实战100】【2024】✨︎ 在本专栏中不仅包含一些适合初学者的最新机器学习项目&#xff0c;每个项目都处理一组不同的问题&#xff0c;包括监督和无监督学习、分类、回归和聚类&#xff0c;而且涉及创建深度学…

数据挖掘实验一

一、实验环境及背景 使用软件&#xff1a; Anaconda3 Jupyter Notebook 实验内容&#xff1a; 1.使用Tushare或者其他手段获取任意两支股票近三个月的交易数据。做出收盘价的变动图像。2.使用Pandas_datareader获取世界银行数据库中美国&#xff08;USA&#xff09;、瑞典&…