学习小部件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" />
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);
}
答案 0 :(得分: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()函数。
答案 1 :(得分:-1)
我在这里写了一个答案Android: How do I force the update of all widgets of a particular kind,它允许您的小部件在被点击时进行更新。这可以让您更好地控制何时执行更新。
答案 2 :(得分:-4)
您只需修改“widget.xml”文件:
android:updatePeriodMillis="15000"
这将导致它在15秒内触发。该数字以毫秒为单位,即1000ms = 1秒......