代码之家  ›  专栏  ›  技术社区  ›  ip696

Android Widget更新一次调用

  •  0
  • ip696  · 技术社区  · 6 年前

    我尝试创建Android小部件:

    public class MyWidget extends AppWidgetProvider {
    
        final String LOG_TAG = "myLogs";
    
        @Override
        public void onEnabled(Context context) {
            super.onEnabled(context);
            Log.d(LOG_TAG, "onEnabled");
        }
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                             int[] appWidgetIds) {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
            Log.d(LOG_TAG, "onUpdate " + Arrays.toString(appWidgetIds));
        }
    }
    

    当我第一次启动应用程序时,我看到日志输出: D/myLogs: onUpdate [8]

    但如果我在那之后按这个小部件就不会发生。我想是的 d/mylogs:onupdate[8] 再打印一次但不是真的。

    0 回复  |  直到 6 年前
        1
  •  0
  •   CommonsWare    6 年前

    但如果我在那之后按这个小部件就不会发生

    对的。你的应用程序小部件没有用户界面,在该用户界面中,你没有设置 PendingIntent 那会触发 onUpdate() (或其他)被召唤。

    例如,这里有一个 AppWidgetProvider 创建用户界面并使用 处理意图 要控制单击:

    /***
      Copyright (c) 2008-2012 CommonsWare, LLC
      Licensed under the Apache License, Version 2.0 (the "License"); you may not
      use this file except in compliance with the License. You may obtain a copy
      of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
      by applicable law or agreed to in writing, software distributed under the
      License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
      OF ANY KIND, either express or implied. See the License for the specific
      language governing permissions and limitations under the License.
    
      Covered in detail in the book _The Busy Coder's Guide to Android Development_
        https://commonsware.com/Android
    */
    
    package com.commonsware.android.appwidget.dice;
    
    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 AppWidget extends AppWidgetProvider {
      private static final int[] IMAGES={R.drawable.die_1,R.drawable.die_2,
                                          R.drawable.die_3,R.drawable.die_4,
                                          R.drawable.die_5,R.drawable.die_6};
    
      @Override
      public void onUpdate(Context ctxt, AppWidgetManager mgr,
                            int[] appWidgetIds) {
        ComponentName me=new ComponentName(ctxt, AppWidget.class);
    
        mgr.updateAppWidget(me, buildUpdate(ctxt, appWidgetIds));
      }
    
      private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
        RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
                                                R.layout.widget);
    
        Intent i=new Intent(ctxt, AppWidget.class);
    
        i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    
        PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , i,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
    
        updateViews.setImageViewResource(R.id.left_die,
                                         IMAGES[(int)(Math.random()*6)]); 
        updateViews.setOnClickPendingIntent(R.id.left_die, pi);
        updateViews.setImageViewResource(R.id.right_die,
                                         IMAGES[(int)(Math.random()*6)]); 
        updateViews.setOnClickPendingIntent(R.id.right_die, pi);
        updateViews.setOnClickPendingIntent(R.id.background, pi);
    
        return updateViews;
      }
    }
    

    (从 this sample app 异形的 this book )

    我的 OnUpDATE() 方法调用 buildUpdate() 方法。 生成更新() :

    • 创建一个 RemoteViews 基于布局,定义app小部件的基本ui
    • 确定两个图像中要显示的图像 ImageView 该布局中的小部件(在本例中,是六个六角模具面的随机图像)
    • 创建一个 Intent 它将调用用于触发我的 OnUpDATE() 呼叫
    • 包覆 意图 在广播中 处理意图
    • 附上 处理意图 到要响应单击事件的用户界面
    • 使用配置的 远程视图 作为应用程序小部件的ui

    结果:当用户单击图像或背景时, AppWidgetProvider程序 再次调用 OnUpDATE() ,用户将看到更新的骰子卷。

    推荐文章