ウィジェットを3日間学習した後、私はついにsetOnClickPendingIntent、RemoteViewsについて理解しています。それは多くのチュートリアルの助けを借りて行われています。しかし、今私はそれが動作するかどうかテストしたいと思います。私が読んでいるように、最小更新レートは30分です。もう一つの方法はAlarmManagerを使用することです。しかし、私はAlarmManagerでどのような例も見つけられません。 私が30分待っても何も起こりませんでした。私はいくつかの事を変えてまだ待っていて、それが変わる...私のウィジェットのテストonUpdateメソッドが30分待っていない方法
もっと速くテストする方法はありますか?
<?xml version="1.0" encoding="utf-8" ?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="65dip"
android:minHeight="30dip"
android:updatePeriodMillis="180000"
android:initialLayout="@layout/main" />
CountWidget
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 CountWidget 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 };
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName me = new ComponentName(context, CountWidget.class);
appWidgetManager
.updateAppWidget(me, buildUpdate(context, appWidgetIds));
// обновляем виджет
// appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
private RemoteViews buildUpdate(Context context, int[] appWidgetIds) {
// Создаем новый RemoteViews
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
remoteViews.setImageViewResource(R.id.left_die, IMAGES[(int) (Math
.random() * 6)]);
// Подготавливаем Intent для Broadcast
Intent configIntent = new Intent(context,
CountWhatYouWantActivity.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
// создаем наше событие
PendingIntent configPendingIntent = PendingIntent.getActivity(context,
0, configIntent, 0);
// регистрируем наше событие
remoteViews.setOnClickPendingIntent(R.id.left_die, configPendingIntent);
return (remoteViews);
}
[AlarmManager.cancel()](http://developer.android.com/reference/android/app/AlarmManager.html)を呼び出すことを忘れないでください。あなたのAppWidgetProviderのonDisabled()メソッドの中の#cancel(android.app.PendingIntent))、またはあなたのアラームは最後のウィジェットが削除された後でもチェックを続けます! – nicopico