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

2024-04-29 13:28

本文主要是介绍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
在这里插入图片描述

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

这篇关于Android Widget开发代码示例详细说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/946213

相关文章

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

zookeeper端口说明及介绍

《zookeeper端口说明及介绍》:本文主要介绍zookeeper端口说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、zookeeper有三个端口(可以修改)aVNMqvZ二、3个端口的作用三、部署时注意总China编程结一、zookeeper有三个端口(可以

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具