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

相关文章

鸿蒙应用开发之调用C++开发代码库1

由于鸿蒙应用主要基于ArkTS开发,主要使用的语言是ETS,也就是JS脚本方式来开发,这与C++语言开发是不同的语言,这样就不能直接相互调用,因为不同的语言使用不同的数据结构,不同的调用方式,不同的运行环境。要想在ETS语言里调用C++开发的代码,需要进行合适的转换,并且按指定的格式来编写,才能够在ArkTS里使用。并且由于ArkTS运行在虚拟机里,需要注意受到调用端线程的限制,不能随便地使用线程

LVS负载均衡超详细入门介绍

LVS 一、LVS入门介绍 1.1.LVS负载均衡简介 1.2.负载均衡的工作模式 1.2.1.地址转换NAT(Network Address Translation) 1.2.2.IP隧道TUN(IP Tunneling) 1.2.3.直接路由DR(Direct Routing) 1.3.LVS常用的负载调度算法 1.4.LVS管理工具ipvsadm 二、NFS

Windows系统下安装配置 MinGW-w64 开发环境

MinGW、MinGW-w64 简介 MinGW(全称为,Minimalist GNU for Windows),它实际上是将经典的开源 C语言编译器 GCC 移植到了 Windows 平台下,并且包含了 Win32API ,因此可以将源代码编译为可在 Windows 中运行的可执行程序。而且还可以使用一些 Windows 平台不具备的,但是Linux平台具备的开发工具和API函数。用一句话来概

可视化低代码平台扫描[01]:阿里云DataV

Hi,本篇开始陆续介绍一些可视化的低代码平台,有使用经验的老铁,可以评论区分享。 一、阿里云dataV 阿里云DataV可以被归类为低代码平台,因为它提供了一系列可视化设计工具和组件,使用户无需编写复杂的代码就能创建和定制数据可视化大屏。用户可以通过简单的拖拽、配置和设置来实现数据的可视化展示,而不需要深入的编程知识。 这种低代码平台的设计理念使得用户能够更快速、更灵活地构建

正则表达式常用特殊字符(元字符)说明

正则表达式中包含多种特殊字符(也称作元字符),它们具有特定的含义,用于构建复杂的匹配模式。以下是一些常用的特殊字符序列及其含义: \d - 匹配任何数字,等同于 [0-9]。\D - 匹配任何非数字字符,等同于 [^0-9]。\w - 匹配任何字母数字字符,包括下划线,等同于 [a-zA-Z0-9_]。\W - 匹配任何非字母数字字符,等同于 [^a-zA-Z0-9_]。\s - 匹配任何空白字

静态代码扫描问题修复之--(输入验证 重定向[NodeJS])

问题描述: 当应用程序直接使用未经验证的用户输入来控制重定向URL时,存在安全隐患。攻击者可借此机会操纵输入,引导用户访问恶意网站,实施网络钓鱼等攻击。这在JavaScript前端代码及Node.js后端逻辑中均需警惕。 案例分析: 应用程序允许未验证的用户输入控制重定向中的URL,可能会导致攻击者发动钓鱼攻击。 例1:以下JavaScript代码从用户输入表单的dest参数中读取目的URL

周围很多朋友都不做java开发了而转换学网络安全,这个值得吗?

在开始前我有一些资料,是我根据网友给的问题精心整理了一份「java的资料从专业入门到高级教程」, 点个关注在评论区回复“888”之后私信回复“888”,全部无偿共享给大家!!! 其实说白了,值不值得取决于你如何看待所选择的行业,总不能脱离实际,盲目追求那些虚无缥缈的东西,你说对吧? 网络安全对于大众来说门槛低入门快,所以这些年吸引了大批人涌入,其中不乏就就有很多所谓的会点工具就觉得自己很

Java二分查找(代码帖)

Java二分查找(代码帖) 怕记不住, 对于一个有序数据,可以使用二分查找,前提是数组必须有序,时间复杂度为O(logn)。 二分查找通常有两种写法:递归、非递归 (1)二分查找递归 /*** 二分查找*/public static int binarySerch(int[] array, int value, int left, int right) {if(left >

Android触屏分发机制(二)

上一篇博客 Android触屏分发机制(一) http://blog.csdn.net/u010429311/article/details/50978987 在上一篇博客中讲到了Button的点击事件处理,即View的点击事件是如何分发的,那么这篇文章更深入的理解触屏分发机制,讲述ViewGroup是如何实现的。大家可能都知道各种布局如RelativeLayout,LinearLayou

Android触屏分发机制(一)

面试学校工作室HCI时曾被要求写类似知乎的界面逻辑,其中遇到了一个问题就是layout的onTouch和button的onClick冲突,解决的方法便是了解触屏分发机制。这段时间查阅了许多资料,具体了解Android的触屏分发机制是怎么实现的。 按钮的点击事件相信大家闭着眼睛都能写得出来了: btn.setOnClickListener(new View.OnClickListener() {