android 之 桌面的小控件AppWidget

时间:2022-07-26 22:46:55

AppWidget是创建的桌面窗口小控件,在这个小控件上允许我们进行一些操作(这个视自己的需要而定)。作为菜鸟,我在这里将介绍一下AppWeight的简单使用。

1.在介绍AppWidget之前,我们先来了解一下PendingIntent和RemoteViews;

PendingIntent:A description of an Intent and target action to perform with it. Instances of this class are created with getActivity, getActivities, getBroadcast, and getService; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

翻译:通过PendingIntent来描述一个Intent和target action,这个类的实例和getActivity,getActivites,getBroadCast,getService一起创建:返回的对象能够传给其他程序,以便它们能在后来执行你想让他们执行的动作。

结合Mars老师的讲解可以这样来理解:PendingIntent能够包含一个Intent对象,能够将这个Intent传递给其他的应用,当满足一定条件时,就会执行这个Intent中包含的动作。getActivity和getActivities是为了启动活动,getBroadcast是为了发送广播,getService是为了开启一个服务。(这个在下面有讲解)

RemoteViews:A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.

翻译:remoteviews:一个类描述了一个视图的层次结构,可以显示在另一个进程。该层次结构从布局资源文件中增长,该类提供一些基本操作来修改虚化层次结构的内容。
简言之就是RemoteView代表的是显示在桌面上的appwidget这整个布局,包括设置的按钮,图片什么的。。。

2.了解了上面的概念,就可以更好的理解AppWeight了。

实现AppWeige的步骤:

文件的结构:

1).在res下新建一个文件夹,里面新建一个类型为appwidget_provider的xml文件。这个是代表显示在桌面上的整个布局(只是布局的框架,不包含细节,至于显示的内容需要引用另一个布局文件),示例中我的xml文件名为example_appwidget_provider.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth = "280dp"
android:minHeight = "100dp"
android:updatePeriodMillis = "864000000"
android:initialLayout="@layout/example_appwidget"> </appwidget-provider>
<!-- 上面设置了布局的宽、高、更新时间周期(毫秒)、包含的布局 -->

  2).在layout中新建一个xml文件来作为在appwidget中的布局显示,就是上面的@layout/example_appwidget中的example_appwidget。

<?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="vertical" > <!-- 这里是显示在桌面的appWidget的布局 -->
<ImageView
android:id="@+id/imageId"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:src="@drawable/cancer"/>
<Button
android:id="@+id/changeButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#00ff00"
android:text="开启另一个活动"/>
</LinearLayout>

  3).新建一个Activity,这个Activity继承AppWidgetProvider,复写其中的方法。在这里注释的代码实现了点击AppWidget中的按钮就启动另一个Activity,未注释的代码实现了ImagView中的Image的替换:值得注意的是,AppWiget所进行的活动并不与当前Main_Activity所在的线程在同一个线程中,因此这里并不能像在主线程中那样操作布局内容,AppWiget的操作方法可以看下面代码。

package com.example.appwight;

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; public class ExampleAppWidgetProvider extends AppWidgetProvider{ //广播名称,需要在AppWidget Manifest.xml文件中声明
private static final String UPDATE_INTENT = "com.example.appwidget"; @Override
public void onReceive(Context context, Intent intent) {
//接受广播的时候调用
    //接受广播Action
String action = intent.getAction();
    //当接受到指定的广播时,就执行下main代码
if (UPDATE_INTENT.equals(action)) {
System.out.println("action=======>"+UPDATE_INTENT); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
       //第一个参数:ImagView的Id,第二个参数:用来替换的图片
remoteViews.setImageViewResource(R.id.imageId, R.drawable.chilli);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//ComponentName也代表AppWidget控件,与RemoteViews不同的是。前者只是代表着“整个”控件,后者包含Button,ImageView,,,等内容
ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}else {
//调用父类的onReceive方法
super.onReceive(context, intent);
}
} @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("onUpdate");
for (int i = 0; i < appWidgetIds.length; i++) { //Intent intent = new Intent(context, SecondActivity.class);
Intent intent = new Intent();
intent.setAction(UPDATE_INTENT);
/*
* pendingIntent可以包含一个intent
* 第一个参数是当前上下文,第二个参数是包含的intent
* getAcitivity表明这个PendingIntent的目的是为了启动一个activity,此外还有getService(),和getBroadcast()
*PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//remoteView代表的是AppWidget的整个布局,第一个参数是包名,第二个参数是AppWidget的布局
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
//为remoteVIew中的按钮设置点击事件,第一个为按钮的id,第二个参数为要启动的PendingIntent。
remoteViews.setOnClickPendingIntent(R.id.changeButton, pendingIntent);
//更新AppWidget,第一个参数为表明是当前哪个AppWidget(因为可以在桌面上创建多个AppWidget)
//第二个参数为要更新的RemoteViews的对象
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
} @Override
public void onDeleted(Context context, int[] appWidgetIds) {
//删除AppWidget的时候调用
super.onDeleted(context, appWidgetIds);
System.out.println("onDeleted");
} @Override
public void onEnabled(Context context) {
//当AppWidget被创建的时候调用
super.onEnabled(context);
System.out.println("onEnabled");
} @Override
public void onDisabled(Context context) {
//当最后一个AppWidget被删除的时候调用
super.onDisabled(context);
System.out.println("onDisable");
} }

  4).在AndroidManifest.xml文件中的<application 中声明。

<receiver
android:name="com.example.appwight.ExampleAppWidgetProvider">
        //过滤广播
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="com.example.appwidget"/>
</intent-filter>
        //元数据,将布局与类绑定
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_provider"/>
</receiver>

效果图:

android 之 桌面的小控件AppWidgetandroid 之 桌面的小控件AppWidget

android 之 桌面的小控件AppWidget的更多相关文章

  1. 怎样在Android实现桌面清理内存简单Widget小控件

    怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  2. Android 布局之LinearLayout 子控件weight权重的作用详析&lpar;转&rpar;

    关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...

  3. Android高效率编码-细节,控件,架包,功能,工具,开源汇总&comma;你想要的这里都有

    Android高效率编码-细节,控件,架包,功能,工具,开源汇总 其实写博客的初衷也并不是说什么分享技术,毕竟咱还只是个小程序员,最大的目的就是对自我的知识积累,以后万一编码的时候断片了,也可以翻出来 ...

  4. Android开发工程师文集-相关控件的讲解,五大布局

    前言 大家好,给大家带来Android开发工程师文集-相关控件的讲解,五大布局的概述,希望你们喜欢 TextView控件 TextView控件有哪些属性: android:id->控件的id a ...

  5. Android 布局之LinearLayout 子控件weight权重的作用详析

    关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...

  6. android中一个评分的控件

    RatingBar android中一个评分的控件 如何使用 Android Studio下: dependencies { compile 'com.hedgehog.ratingbar:app:1 ...

  7. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  8. Android 开源组件 ----- Android LoopView无限自动轮转控件

    Android 开源组件 ----- Android LoopView无限自动轮转控件 2015-12-28 15:26 by 杰瑞教育, 32 阅读, 0 评论, 收藏, 编辑 一.组件介绍 App ...

  9. android学习日记03--常用控件button&sol;imagebutton

    常用控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件的一些简单而可见的功能.所有控件都是继承View类 介绍android原生提供几种常用的控件bu ...

随机推荐

  1. 转&colon;Android随笔之——使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)

    本文转载自CSDN的jzj1993,原文连接:http://blog.csdn.net/jzj1993/article/details/39158865 有时我们需要使用安卓实现在后台模拟系统按键,比 ...

  2. C&plus;&plus;模板实例化

    深入理解C++中第七章提到模板实例化参数的选择:函数的决议结果只和函数参数有关和返回值无关.记录一下. 测试程序如下: #include <iostream> using namespac ...

  3. Interleaving String leetcode java

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...

  4. Shell之sed用法 转滴

    通过例子学习sed的用法 1,sed介绍    sed可删除(delete).改变(change).添加(append).插入(insert).合.交换文件中的资料行,或读入其它档的资料到 文> ...

  5. 如何出色的研究 RGSS3 (三) 形式的调整的细节

    在一个我们研究了添加到窗体方法的选择,这个问题来研究窗体类的细节. 所有形式的父类的 Window_Base 四个参数需要初始化. #--------------------------------- ...

  6. 界面渐变特效 -- CSS实现 -- 兼容IE8

    特别注意:里面的RGB颜色值必须要全写,不能使用缩写.左右:background: -webkit-gradient(linear, 0 0, 0 100%, from(#80c1e7), to(#2 ...

  7. 老司机心得之时间管理&quot&semi;入坑&quot&semi;

    长期以来,时间管理一直被认为是自我管理,团队管理,项目管理的既关键又基础的手段,就连笔者本人也一直在崇尚时间管理的理念. 但是这里要讲的,不是什么鬼神方法论.而主要是对长时间以来学习和实践时间管理的一 ...

  8. git 20181119

    不同分支间(a.b分支)合并部分文件 git ck b git ck a f1 f2 git配置 配置文件 系统git config --system --list 全局git config --gl ...

  9. 查看mysql中sql语句执行时间

    查看mysql版本:select version();方法一: show profiles.1. Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后.  ...

  10. redis aof和rdb区别

    转自https://blog.csdn.net/m0_38110132/article/details/76906422 1.前言 最近在项目中使用到Redis做缓存,方便多个业务进程之间共享数据.由 ...