[英]Widget onUpdate calls once
我嘗試創建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]
再次打印,但事實並非如此。
但是,如果我按此小部件,那不會發生
正確。 您的應用程序小部件沒有UI,並且在該UI中您沒有設置會觸發onUpdate()
(或其他任何東西)的PendingIntent
。
例如,下面是一個AppWidgetProvider
,它創建一個UI並使用PendingIntent
來控制點擊次數:
/***
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;
}
}
我的onUpdate()
方法調出一個buildUpdate()
方法。 buildUpdate()
:
RemoteViews
,以定義應用程序小部件的基本UI ImageView
小部件中顯示的ImageView
(在這種情況下,是六個六模面的隨機圖像) Intent
,該Intent
將調用與觸發onUpdate()
調用相同的廣播 PendingIntent
包裝該Intent
PendingIntent
附加到UI以響應點擊事件 RemoteViews
配置為應用程序小部件的UI 結果:當用戶單擊圖像或背景時,將使用onUpdate()
再次調用AppWidgetProvider
,並且用戶會看到骰子的更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.