Android Widget不会更新多次

时间:2017-08-22 09:04:09

标签: java android service widget

您好我有一个简单的小部件显示消息未读计数,我每隔几秒触发一次小部件更新(通过服务和AppWidgetProvider),并且每隔几秒使用新值正确调用widgetProvider onUpdate,但小部件视图不会更新首次更新后不止一次,可能导致什么问题? 这是onUpdate代码,它是TRIGGERED但不能多次运行

for (int i = 0; i < count; i++) {
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_widget_count);
        if (unreadCount > 0) {
            remoteViews.setViewVisibility(R.id.notification_widget_count_root, View.VISIBLE);
            //  remoteViews.setImageViewBitmap(R.id.notification_widget_count_image_text, M.getFontBitmap(context, unreadText, context.getResources().getColor(R.color.white), 25));
            remoteViews.setTextViewText(R.id.notification_widget_count_text, new Date().getSeconds()+"");
            Intent mainIntent = new Intent(context, MainActivity.class);
            mainIntent.setAction(ACTION_WIDGET_CLICK_MAIN);
            PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
            remoteViews.setOnClickPendingIntent(R.id.notification_widget_count_main_root, mainPendingIntent);
            manager.updateAppWidget(appWidgetIds[i],remoteViews);
            Log.d("FFFF-NotificationWidget", "Updating Widget Info...");
        } else {
            remoteViews.setViewVisibility(R.id.notification_widget_count_root, View.GONE);
            manager.updateAppWidget(appWidgetIds[i], remoteViews);
        }

提前感谢您的帮助

2 个答案:

答案 0 :(得分:2)

如果您使用服务,请尝试此

<banner-component></banner-component>
<md-card class="card">
    ...
</md-card>

答案 1 :(得分:0)

虽然你将android:updatePeriodMillis设置为20秒,但最小实际时间是30分钟。因此,如果您在测试期间没有等待30分钟查看是否有更新,则需要等待一段时间。