android - 如何测试我的小部件的 onUpdate 方法不等待 30 分钟

标签 android widget onupdate

经过 3 天的小部件学习,我终于了解了 setOnClickPendingIntent、RemoteViews...,并且完成了我的小部件。它是在许多教程的帮助下完成的。但现在我想测试它是否有效。在我阅读时,最小更新率为 30 分钟。另一种方法是使用 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);
    }

最佳答案

这可能有帮助:

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() 函数。

关于android - 如何测试我的小部件的 onUpdate 方法不等待 30 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6807500/

相关文章:

android - 启用 Proguard 时出错

qt - 如何正确配置QDockWidget使其显示/隐藏?

c++ - CRichEditCtrl OnUpdate() : how to know the start and end positions when a paste is received?

android - 当后台 "sleeping"应用程序过多时,Android 操作系统会发生什么情况?

android - Websockets 上的视频流

android - 当应用程序处于后台时,变量保持其值多长时间?

php - 寻找一个 jQuery + AJAX + PHP5/PDO + MySQL Recordset Pagination Widget

c++ - Qt 的.NET 属性表?有什么建议么?

由于核心数据迁移,iOS 应用程序在启动屏幕上崩溃