AppWidget组件的处理事件

2024-02-20 10:38

本文主要是介绍AppWidget组件的处理事件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

网站转载


在appWidget中,ImageButton和Button都是被支持的控件,其事件可分成三种类型:

一、开启Activity
二、开始Service
三、发送按钮Action
下面开始一个一个分析,如何实现。

一、开启Activity
1、首先先定义个开启Activity的intent
eg:  
Intent fullIntent=new Intent(this,FullScreen.class);
若要传递数据,则使用intent.putExtra()方法
eg:  fullIntent.putExtra("isCircle",isCircle);

2、用intent实例化一个PendingIntent,调用pendingIntent的getActicity方法来启动另一个Activity
①若该Intent带有数据,则需要将最后一个参数的值设为:FLAG_CANCEL_CURRENT
eg:  PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, fullIntent,PendingIntent.FLAG_CANCEL_CURRENT);
②若该Intent不带数据,则最后一个参数设为0
eg:  PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, fullIntent, 0);

3、实例化RemoteView,其对应相应的Widget布局
eg:  RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);

4、给RemoteView上的Button或ImageButton设置按钮事件
eg: views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);

5、更新AppWidget界面
①如果是在onUpdate()方法内更新AppWidget界面
eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);
②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量
eg:  public RemoteViews views;//RemoteView对象
     publicComponentName thisWidget; //组件名
    public AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(this,PictureAppWidgetProvider.class);
    manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, views);

二、开启Service
1、定义一个intent来开启Service
eg:  Intent startServiceInten=newIntent("zyf.temp.Service.START");
注:参数为开启Service的动作

2、用Intent实例化一个PendingIntent,利用PendingIntent的getService方法来启动一个服务
eg:  PendingIntent Pintent=PendingIntent.getService(context, 0, startServiceInten, 0);

3、实例化RemoteView,其对应相应的Widget布局
eg:  RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);

4、给RemoteView上的Button或ImageButton设置按钮事件
eg: views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);

5、更新AppWidget界面
①如果是在onUpdate()方法内更新AppWidget界面
eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);
②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量
eg:  public RemoteViews views;//RemoteView对象
     publicComponentName thisWidget; //组件名
    public AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(this,PictureAppWidgetProvider.class);
    manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, views);

三、发送按钮Action
1、定义一个Intent来发送按钮Action
eg:  Intent prevInten=new Intent("PREV");

2、用Intent实例化一个PendingIntent,利用PendingIntent的getBroadcast方法来发送广播
eg:  PendingIntent Pprevintent=PendingIntent.getBroadcast(this, 0, prevInten, 0);

3、实例化RemoteView,其对应相应的Widget布局
eg:  RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);

4、给RemoteView上的Button或ImageButton设置按钮事件
eg:  views.setOnClickPendingIntent(R.id.IBprev,Pprevintent);

5、更新AppWidget界面
①如果是在onUpdate()方法内更新AppWidget界面
eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);
②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量
eg:  public RemoteViews views;//RemoteView对象
     publicComponentName thisWidget; //组件名
    public AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(this,PictureAppWidgetProvider.class);
    manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, views);

6、接收该Action
①在AppWidget自己的onReceive方法内接收
⒈在Action,要在Manifest.xml中加入Action
eg: <intent-filter>
     <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
     <actionandroid:name="PREV"></action>
   </intent-filter>
⒉在onReceive()方法内编写要实现的动作
eg:  if(intent.getAction().equals("PREV"))
{
   //在这编写接收到该Action后要实现的动作
}
②在Service内接收
⒈注册一个BroadcastReceive,声明接收器
eg:  IntentFilter filter=new IntentFilter();
    filter.addAction("PREV");
    registerReceiver(doCommand, filter);
⒉,在BroadcastReceive类的onReceive方法内编写要实现的动作
eg:  if(intent.getAction().equals("PREV"))
{
   //在这编写接收到该Action后要实现的动作
}

这篇关于AppWidget组件的处理事件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl