窗口小部件上的TextView不使用OnUpdate方法更新

时间:2013-02-17 22:16:23

标签: android android-widget broadcastreceiver textview

我有一个显示使用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) {

}

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以注册BroadcastReceiver并收听ACTION_TIME_TICK操作。

每分钟都会调用这个。

BroadcastReceiver mBroadcastTime = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Change the time on your widget
        }
    };

IntentFilter mTime = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(mBroadcastTime, mTime);

答案 1 :(得分:0)

您必须使用AlaramManager来实现此功能。

我在我的时钟部件中以下列方式使用。

alaram的倒计时:

 class CountSecond extends CountDownTimer
        {
            public CountSecond(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
                // TODO Auto-generated constructor stub

                Intent intent = new Intent(ClockWidget.CLOCK_WIDGET_UPDATE);
                 intent.putExtra("theme", SelectedTheme);
                sendBroadcast(intent);
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                Log.d("CL1 Service ","--- count finish ");
                //setTimertoUpdate();


                AlarmManager alarmManager = (AlarmManager)ClockWidgetSetting.this.getSystemService(Context.ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                //calendar.add(Calendar.MINUTE, 1);
                alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 60000, createClockTickIntent(ClockWidgetSetting.this));
                //finish();

            }

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
            }

        }

打算调用小部件appwidgetprovider的意图如下。这样就可以调用onUpdate()小部件。

 private PendingIntent createClockTickIntent(Context context) {
            Intent intent = new Intent(ClockWidget.CLOCK_WIDGET_UPDATE);
            intent.putExtra("theme", SelectedTheme);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            return pendingIntent;
        }