• Android-Widget桌面小组件


    1,

    掌握Widget的用:
    Widget的用途,能够添加到手机桌面的程序

    2,

    Widget的特点和用法步骤:

    特点:快捷,方便,个性化,可自定义功能,可及时控制更新Widget显示内容

    3,

     用法步骤流程图

    1. 绘制Widget布局
    2. 配置Widget的基本属性
    3. 定义AppWidgetProvider
    4. 提供Configuration Activity

    4,

    Android Widget实现一个基础的时钟功能

    效果:

    • 可以自动更新时间
    • 可以添加和删除时钟Widget

    5,

    完整代码:

    package com.example.widget;
    
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.RemoteViews;
    
    /**
     * 类似广播,是桌面小组件Widget的Provider
     */
    public class widget extends AppWidgetProvider {
    
        static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                    int appWidgetId) {
            CharSequence widgetText = context.getString(R.string.appwidget_text);
            // Construct the RemoteViews object
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setTextViewText(R.id.tv, widgetText);
            // Instruct the widget manager to update the widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    
        //刷新widget的时候执行,remoteView和AppWidgetManager
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            // There may be multiple widgets active, so update all of them
            for (int appWidgetId : appWidgetIds) {
                updateAppWidget(context, appWidgetManager, appWidgetId);
            }
        }
    
        //第一个widget添加到屏幕上执行
        @Override
        public void onEnabled(Context context) {
            // Enter relevant functionality for when the first widget is created
            super.onEnabled(context);
            //启动Service
            context.startService(new Intent(context, TimerService.class));
        }
    
        //最后一个widget被从屏幕移除执行
        @Override
        public void onDisabled(Context context) {
            // Enter relevant functionality for when the last widget is disabled
            super.onDisabled(context);
            context.stopService(new Intent(context, TimerService.class));
        }
    
        //Widget被从屏幕移除
        @Override
        public void onDeleted(Context context, int[] appWidgetIds) {
            super.onDeleted(context, appWidgetIds);
        }
    
        //这个方法一般不会重写,无论操作中间(上面)的哪些方法,都会执行onReceive
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
        }
    
    }
    

     service:

    package com.example.widget;
    
    import android.app.Service;
    import android.appwidget.AppWidgetManager;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.os.IBinder;
    import android.widget.RemoteViews;
    
    import androidx.annotation.Nullable;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimerService extends Service {
    
        //java.util.Timer下的定时器
        private Timer timer;
    
        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateViews();
                }
            }, 0, 1000);
        }
    
        private void updateViews() {
            String time = sdf.format(new Date());
            RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget);
            rv.setTextViewText(R.id.tv, "黄朝阳666 : " + time);
            AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
            ComponentName cn = new ComponentName(getApplicationContext(), widget.class);
            manager.updateAppWidget(cn, rv);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            timer = null;
        }
    
    }

    6,

    添加和删除天气Widget:

    7,

    8,

  • 相关阅读:
    imagemagick 批量旋转图片 转为横版式
    visual studio 2008 编译 filezilla
    BackgroundWorker 类
    序列化
    打工才是最愚蠢的投资
    常用汇编指令缩写(方便记忆)
    PIC中档单片机汇编指令详解(1)
    Win8之自动登录
    PIC单片机汇编指令
    好文转载—程序员的禅修之路
  • 原文地址:https://www.cnblogs.com/yang101/p/11595790.html
Copyright © 2020-2023  润新知