在屏幕上加入Widget:或长按屏幕空白处,或找到WidgetPreview App选择。

原生系统4.0下面使用长按方式,4.0及以上 打开WIDGETS



创建Widget的一般步骤:

在menifest中

<receiver android:name="com.stone.ui.TimerWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<!-- 自己定义action -->
<action android:name="com.stone.action.start"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/timer_widget_provider"/>
<!-- android:resource="" 定义了Widget的信息使用timer_widget_provider.xml描写叙述 -->
</receiver>

Widget描写叙述文件:timer_widget_provider.xml

<?

xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="180dp"
android:minHeight="40dp"
android:updatePeriodMillis="5000"
android:previewImage="@drawable/ic_launcher"
android:initialLayout="@layout/timer_widget"
android:resizeMode="horizontal|vertical"
android:widgetCategory="keyguard|home_screen"
android:configure="com.stone.ui.AppWidgetConfigureActivity" >
<!--
计算size的公式: (70*n) -30 n为部件所需的大小(占几格) 当前的就是 3X1
minResizeWidth
minResizeHeight 能被调整的最小宽高,若大于minWidth minHeight 则忽略
label 选择部件时看到标签
icon 选择部件时看到图标
updatePeriodMillis 更新时间间隔
previewImage 选择部件时 展示的图像 3.0以上使用
initialLayout 布局文件
resizeMode 调整size模式
configure 假设须要在启动前先启动一个Activity进行设置,在这里给出Activity的完整类名
widgetCategory="keyguard|home_screen" widget可加入的位置 锁屏界面|桌面 autoAdvanceViewId=@id/xx 与集合部件一起使用。指定该id所表示的集合的item自己主动推进 集合部件:3.0后才有。view:ListView、GridView、StackView、AdapterViewFilpper
-->
</appwidget-provider>

Widget使用的布局:timer_widget.xml

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/widget_background" > <TextView
android:id="@+id/counter"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="00:00"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearance"/>
<ImageView
android:id="@+id/start_stop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:src="@android:drawable/ic_media_play"/>
</LinearLayout>

处理Widget相关事件的 AppWidgetProvider

package com.stone.ui;

import java.util.Arrays;

import com.stone.R;
import com.stone.service.TimerWidgetService; import android.app.PendingIntent;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.widget.RemoteViews; /*
* AppWidgetProvider 必需要接收android.appwidget.action.APPWIDGET_UPDATE 广播
*
*/
public class TimerWidgetProvider extends AppWidgetProvider { @Override //更新部件时调用,在第1次加入部件时也会调用
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("onUpdate widget:" + Arrays.toString(appWidgetIds));
/*
* 构造pendingIntent发广播,onReceive()依据收到的广播,更新
*/
Intent intent = new Intent(context, TimerWidgetService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.timer_widget);
rv.setOnClickPendingIntent(R.id.start_stop, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, rv);
} @Override //部件从host中删除
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
System.out.println("onDeleted widget");
} @Override //第1次创建时调用。之后再创建不会调用
public void onEnabled(Context context) {
super.onEnabled(context);
System.out.println("onEnabled widget");
} @Override //当最后一个部件实例 被删除时 调用 用于清除onEnabled运行的操作
public void onDisabled(Context context) {
super.onDisabled(context);
System.out.println("onDisabled widget");
} @Override //
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
System.out.println("onReceive widget");
/*
* 接收 <action android:name="com.stone.action.start"/>
在其它组件或activity或service中发送这些广播 */
if (intent.getAction().equals("com.stone.action.start")) {
long time = intent.getLongExtra("time", 0);
updateWidget(context, time);
System.out.println("receive com.stone.action.start");
}
} private void updateWidget(Context context, long time) {
//RemoteViews处理异进程中的View
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.timer_widget);
System.out.println("time=" + time);
rv.setTextViewText(R.id.counter, DateUtils.formatElapsedTime(time / 1000)); AppWidgetManager am = AppWidgetManager.getInstance(context);
int[] appWidgetIds = am.getAppWidgetIds(new ComponentName(context, TimerWidgetProvider.class));
am.updateAppWidget(appWidgetIds, rv);//更新 全部实例
}
}

AppWidgetProvider中使用的TimerWidgetService

package com.stone.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class TimerWidgetService extends Service { @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
/*
* do some thing
*/
//发送广播通知 widget 更新 状态
sendBroadcast(new Intent("com.stone.action.start").putExtra("time", System.currentTimeMillis()));
return Service.START_STICKY;
}
}

Android Widget 小部件(一) 简单实现的更多相关文章

  1. Android Widget 小部件(四---完结) 使用ListView、GridView、StackView、ViewFlipper展示Widget

    官方有话这样说: A RemoteViews object (and, consequently, an App Widget) can support the following layout cl ...

  2. Android Widget 小部件(三) 在Activity中加入Widget

    package com.stone.ui; import static android.util.Log.d; import android.app.Activity; import android. ...

  3. Android简易实战教程--第十四话《模仿金山助手创建桌面Widget小部件》

    打开谷歌api,对widget小部件做如下说明: App Widgets are miniature application views that can be embedded in otherap ...

  4. Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的!

    Android Widget小组件开发(一)--Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的! PS:学习自某网站(不打广告) 这个小组件相信大家都很熟悉吧,以前的墨迹天气 ...

  5. 桌面小部件AppWidgetProvider简单分析

    1.一般桌面小部件涉及到的类 AppWidgetProvider :BroadcastRecevier子类,用于接收更新,删除通知 AppWidgetProvderInfo:AppWidget相关信息 ...

  6. Android 桌面小部件

    1. 添加AppWidgetProvider 实际上就是个带有界面的BroadcastReceiver public class SimpleWidgetProvider extends AppWid ...

  7. 从Hello World说起(Dart)到“几乎所有东西都是Widget”小部件。

    import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...

  8. Android Widget 小工具(两) 使用configure

    添加Widget在此之前需要做一些处理操作,可以使用 配置活动 在上一篇的实现基础上,加上配置活动(configure=activity).这时加入Widget时.会先打开一个Activity,进行配 ...

  9. Odoo14 自定义widget小部件

    不多说先上源码. 1 odoo.define('my_company_users_widget', function (require) { 2 "use strict"; 3 4 ...

随机推荐

  1. POJ 2151 Check the difficulty of problems (动态规划-可能DP)

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4522   ...

  2. poj 1011 Sticks ,剪枝神题

    木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿 ...

  3. 10招让你成为杰出的Java程序员(转)

    如果你是一个热衷于技术的 Java 程序员, 那么下面的 10 个要点可以让你在众多 Java 开发人员中脱颖而出. 1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 Ob ...

  4. 在Apache上架设SVN使得可以通过http来使用SVN

    弄了一下午,终于搞定了.找到一篇好的博客.分享出来: 宇哥搞了个论坛网站,我的svn使用不了了,我把svn重新架设到apache后,又可以通过http访问svn了. .安装 Apache http:/ ...

  5. sharepoint 2013 userprofile 用户信息

    Sharepoint2013获得当前用户userfrofile 基本介绍: 什么使用户配置文件. 用户属性和用户配置文件属性提供有关 SharePoint 用户的信息,如显示名称.电子邮件.标题以及其 ...

  6. XSS学习笔记(一个)-点击劫持

    所谓XSS这个场景被触发XSS地方,在大多数情况下,攻击者被嵌入在网页中(问题)该恶意脚本(Cross site Scripting),这里的攻击始终触发浏览器端,攻击的者的目的.一般都是获取用户的C ...

  7. 将鼠标移到文本弹出一些字幕CSS达到,不及格JS达到

    使用css实施内容流行,否js代码,下面的代码,可直接使用复制, 需要背景图到下面的地址下载,谢谢! 住址:http://download.csdn.net/detail/zurich1979/722 ...

  8. poj1236 有向图加边变成强连通图

    给我们一个有向图,有两个问题 1.最少要给多少个点发消息,才能使得所有的点都收到消息(消息可以随边传递) 2.最少需要多少条边才能使得图变成强连通图 对于一个强连通分量,可以当做一个点来考虑,所以我们 ...

  9. python学习笔记之二:使用字符串

    这里会介绍如何使用字符串格式化其他的值,并了解一下利用字符串的分割,连接,搜索等方法能做些什么. 1.基本字符串操作 所有标准的序列操作(索引,分片,乘法,判断成员资格,求长度,取最大值和最小值)对字 ...

  10. UVALive 3890 Most Distant Point from the Sea(凸包最大内接园)

    一个n个点的凸多边形,求多边形中离多边形边界最远的距离.实际上就是求凸包最大内接圆的半径. 利用半平面交求解,每次二分枚举半径d,然后将凸包每条边所代表的半平面沿其垂直单位法向量平移d,看所有平移后的 ...