开发者

How test onUpdate method of my widget not waiting 30min

After 3 days learning widgets, I finally understand about setOnClickPendingIntent,RemoteViews... and I've done my widget. It has been done with help of a lot of tutorials. But now I'd like to test if it works. As I read, the minimum update rate is 30min. And the other way is using AlarmManager. But I cant find any example with AlarmManager. After I waited 30min, nothing happened. I changed some things and still waiting, that it changes...

Is there any way to test it faster?

 <?xml version="1.0" encoding="utf-8" ?>
    <appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="65dip"
    android:minHeight="30dip"

    an开发者_StackOverflowdroid: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);
    }


This might help:

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

}

Call the setAlarm() function in your onEnabled() method of the AppWidgetProvider.


I have written an answer here Android: How do I force the update of all widgets of a particular kind that allows your widget to be updated whenever it is clicked. This can give you greater control of when an update is performed.


You should simply modify your "widget.xml" file:

android:updatePeriodMillis="15000"

This will cause it to fire in 15 seconds. The number is in millseconds, ie, 1000ms=1sec...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜