我有一个使用 TextView 显示数字时钟的小部件,我希望在其中比 xml 中允许的 30 分钟更频繁地更新。我看过一些例子,但我喜欢有人帮助我解决我的具体问题。该小部件配置正确并且工作正常,但不会像它应该的那样更新,因为它是一个数字时钟。
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
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);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.digitalclock);
views.setOnClickPendingIntent(R.id.gl, pendingIntent);
java.util.Date noteTS = Calendar.getInstance().getTime();
String time = "kk:mm";
views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
String date = "dd MMMMM yyyy";
views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
xml android:updateperiodmills 仅每 30 分钟更新一次,但我需要至少每秒更新或设置 TextView,就像我在主应用程序中使用 run 方法完成的一样。非常感谢任何帮助。谢谢
编辑
public class DigitalClock extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
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);
final RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.digitalclock);
views.setOnClickPendingIntent(R.id.gl, pendingIntent);
final java.util.Date noteTS = Calendar.getInstance().getTime();
final String time = "kk:mm";
final String date = "dd MMMMM yyyy";
BroadcastReceiver mBroadcastTime = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
views.setTextViewText(R.id.tvTime,
DateFormat.format(time, noteTS));
views.setTextViewText(R.id.tvDate,
DateFormat.format(date, noteTS));
}
};
IntentFilter mTime = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(mBroadcastTime, mTime);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
private void registerReceiver(BroadcastReceiver mBroadcastTime,
IntentFilter mTime) {
}
}
这现在是我的课,但我不得不添加这个方法:
private void registerReceiver(BroadcastReceiver mBroadcastTime,
IntentFilter mTime) {
}
我该怎么办?