Android Widget 小部件(一) 简单实现
在屏幕上加入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 小部件(一) 简单实现的更多相关文章
- Android Widget 小部件(四---完结) 使用ListView、GridView、StackView、ViewFlipper展示Widget
官方有话这样说: A RemoteViews object (and, consequently, an App Widget) can support the following layout cl ...
- Android Widget 小部件(三) 在Activity中加入Widget
package com.stone.ui; import static android.util.Log.d; import android.app.Activity; import android. ...
- Android简易实战教程--第十四话《模仿金山助手创建桌面Widget小部件》
打开谷歌api,对widget小部件做如下说明: App Widgets are miniature application views that can be embedded in otherap ...
- Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的!
Android Widget小组件开发(一)--Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的! PS:学习自某网站(不打广告) 这个小组件相信大家都很熟悉吧,以前的墨迹天气 ...
- 桌面小部件AppWidgetProvider简单分析
1.一般桌面小部件涉及到的类 AppWidgetProvider :BroadcastRecevier子类,用于接收更新,删除通知 AppWidgetProvderInfo:AppWidget相关信息 ...
- Android 桌面小部件
1. 添加AppWidgetProvider 实际上就是个带有界面的BroadcastReceiver public class SimpleWidgetProvider extends AppWid ...
- 从Hello World说起(Dart)到“几乎所有东西都是Widget”小部件。
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...
- Android Widget 小工具(两) 使用configure
添加Widget在此之前需要做一些处理操作,可以使用 配置活动 在上一篇的实现基础上,加上配置活动(configure=activity).这时加入Widget时.会先打开一个Activity,进行配 ...
- Odoo14 自定义widget小部件
不多说先上源码. 1 odoo.define('my_company_users_widget', function (require) { 2 "use strict"; 3 4 ...
随机推荐
- mongodb时间戳转换成格式化时间戳
db.pay_order.find({"id":"5332336532"},{"tradeNo":true,"status&quo ...
- linux通过使用mail发送电子邮件
通过外部方法发送的电子邮件 bin/mail默认为本地sendmail发送电子邮件,求本地的机器必须安装和启动Sendmail服务.配置很麻烦,并且会带来不必要的 资源占用.而通过改动配置文件能够使用 ...
- Powershell Mail module, 发送outbox 里的全部邮件(一个.csv文件代表一封邮件)
把creating mail代码写到调用处,往outbox写入 mailxxx.csv文件,入面记录了邮件的主要内容 写入 #template $TMP = IMPORT-CSV "$($d ...
- 2014-5-22 java.lang.OutOfMemoryError: Java heap space的一次诊断
收到消息某系统一个节点因为内存溢出而宕机.系统的中间件是weblogic.数据库的oracle. 1. 先用IBM HeapAnalyzer分析内存溢出时的dump文件,找到占用内存最多的请求,然后 ...
- 检查Linux Bash安全漏洞以及各环境修复解决方法
第一.检测是否存在Bash漏洞$env x='() { :;}; echo vulnerable' bash -c "echo this is a test"如果返回以下内容:则请 ...
- 嗨翻C语言
<嗨翻C语言> 基本信息 作者: (美)David Griffiths Dawn Griffiths 译者: 程亦超 出版社:人民邮电出版社 ISBN:978711531884 ...
- Codeforces Round #248 (Div. 1)——Ryouko's Memory Note
题目连接 题意: 给n和m,一行m个1<=x<=n的数.记c=.如今仅仅能选择一个数x变成y,序列中全部等于x的值都变成y,求最小的c 分析: 对于一个数x,把与他相邻的所有的非x的数所有 ...
- SQL语句查询数据库的触发器、存储过程、视图以及表的SQL语句
Sql Server数据库用SQL语句查询方法如下: select name from sysobjects where xtype='TR' --所有触发器 select name from sys ...
- c/c++中main函数参数讲解
参考地址: http://blog.csdn.net/cnctloveyu/article/details/3905720 我们经常用的main函数都是不带参数的.因此main 后的括号都是空括号.实 ...
- JAVA逆向&反混淆-追查Burpsuite的破解原理(转)
0x00 摘要: 本系列文章通过对BurpLoader的几个版本的逆向分析,分析Burpsuite的破解原理,分析Burpsuite认证体系存在的安全漏洞. 0x01 JD-GUI的用途与缺陷: JD ...