7

ウィジェットを 3 日間学習した後、ようやく setOnClickPendingIntent,RemoteViews... について理解し、ウィジェットを完成させました。それは多くのチュートリアルの助けを借りて行われました。しかし、今、それが機能するかどうかをテストしたいと思います。私が読んだように、最小更新レートは 30 分です。もう 1 つの方法は、AlarmManager を使用することです。しかし、AlarmManager の例は見つかりません。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" />

カウントウィジェット

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);
    }
4

3 に答える 3

1

これは役立つかもしれません:

private static Intent updateIntent = new Intent();
{
    updateIntent.setAction(WidgetProvider.ACTION_WIDGET_UPDATE);
    updateIntent.setData(Uri.withAppendedPath(Uri.parse(WidgetProvider.URI_SCHEME + "://widget/id/"), ""));
}

private void setAlarm(Context context) {
    PendingIntent newPending = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Time now = new Time();
    now.setToNow();
    long nowMillis = now.toMillis(false);
    long nextMillis = ((nowMillis / 60000) * 60000) + 60000;

    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    // set the repeating real time clock minute synchronised alarm
    alarms.setRepeating(AlarmManager.RTC_WAKEUP, nextMillis, 60 * 1000, newPending);

}

AppWidgetProvider の onEnabled() メソッドで setAlarm() 関数を呼び出します。

于 2011-09-02T05:24:16.637 に答える
-1

私はここに答えを書きましたAndroid: How do I force the update of all widgets of a specific kind to allow your widget to update when it is clicked. これにより、更新をいつ実行するかをより詳細に制御できます。

于 2011-11-29T01:32:08.557 に答える
-4

「widget.xml」ファイルを変更するだけです。

android:updatePeriodMillis="15000"

これにより、15 秒で発射されます。数値はミリ秒単位です。つまり、1000 ミリ秒 = 1 秒です...

于 2014-09-14T22:50:56.533 に答える