zoukankan      html  css  js  c++  java
  • Android中Widget开发步骤

    一、创建一个类,继承自 AppWidgetProvider
    生命周期介绍:

    onEnabled():创建第一个widget时调用
    onDisabled():删除最后一个widget时调用
    二、在清单文件中,声明一个receiver
    2.1 声明intent-filter的action

    2.2 声明meta-data


    2.3 根据resource布局widget

    三、创建一个服务类,动态(定时)更新widget中的数据
    备注:服务在第一步创建类中生命周期的onEnabled方法中开启,在onDisabled方法中关闭

    3.1 在服务的onCreate()方法中,使用AppWidgetManager.getInstance(getApplicationContext());获取widget管理器对象

    3.2 调用widgetManager中的updateAppWidget方法,更新widget

    // provider:要更新widget的类名
    ComponentName provider = new ComponentName(getApplicationContext(), ExampleAppWidgetProvider.class);

    // views:远程界面,用于获取widget中的控件,来更新控件中的数据
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.process_widget);

    // 给RemoteViews的子组件赋值
    views.setTextViewText(R.id.process_count, "sample_data");

    // 自定义一个广播接收者,在这里发送广播,接收到广播以后完成指定的后台任务
    Intent intent = new Intent("com.mzzdxt.widget.cleartask");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent , 0);

    // 给widget按钮加点击事件
    views.setOnClickPendingIntent(R.id.btn_clear, pendingIntent);

    void android.appwidget.AppWidgetManager.updateAppWidget(ComponentName provider, RemoteViews views)

  • 相关阅读:
    学习笔记2-查看目录文件
    学习笔记1-基本信息及相关目录
    【图论】二分图最大匹配
    【图论】Dinic算法
    【图论】最小割
    【数据结构】左偏树
    【数学】欧拉定理
    【数据结构】ST表
    【数学】博弈模型
    【字符串】后缀数组
  • 原文地址:https://www.cnblogs.com/coderwjq/p/6549727.html
Copyright © 2011-2022 走看看