2

我目前有一个使用两个的小部件setTextViewText

公共类 DigitalClock 扩展 AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    int N = appWidgetIds.length;

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.digitalclock);

    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        Intent clockIntent = new Intent(context, DeskClock.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clockIntent, 0);

        views.setOnClickPendingIntent(R.id.rl, pendingIntent);
        java.util.Date noteTS = Calendar.getInstance().getTime();
        String time = "kk:mm";
        String date = "dd MMMMM yyyy";

        views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
        views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));

        appWidgetManager.updateAppWidget(appWidgetId, views);


    }

}
}

我的问题是如何使用它来TextView每秒设置两个,以便时间更新而不是appwidgetprovider允许的 30 分钟?我听说过 using AlarmManager, using a service, handler.... 问题是似乎没有人说清楚或举例说明我将如何去做。

因此,如果有人有明确的方法来实现这一点,我将不胜感激,谢谢

4

1 回答 1

3

您可能有一些服务在后台运行。将广播消息从该服务或活动发送到扩展 AppWidgetProvider 的类(在您的情况下为 DigitalClock),然后覆盖同一类的 onReceive() 函数。

第 1 步:从服务或某处发送广播。

第 2 步:在您的 DigitalClock 类中添加 onReceive() 函数。

    public class DigitalClock extends AppWidgetProvider {


        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        int N = appWidgetIds.length;

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.digitalclock);

        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent clockIntent = new Intent(context, DeskClock.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clockIntent, 0);

            views.setOnClickPendingIntent(R.id.rl, pendingIntent);
            java.util.Date noteTS = Calendar.getInstance().getTime();
            String time = "kk:mm";
            String date = "dd MMMMM yyyy";

            views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
            views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));

            appWidgetManager.updateAppWidget(appWidgetId, views);


        }

    }
  @Override
        public void onReceive(Context context, Intent intent) {


                super.onReceive(context, intent);
//Do your task here like updating the textviews
}

    }
于 2013-03-10T15:31:04.793 回答