android widget更新,Android Widget不会更新

我一直在研究

Android的小部件.它应该做的一件事是显示当月的当天和当月的一天.我认为我的代码没问题,但由于某种原因它永远不会更新.我的提供商的更新期限设置为30分钟,但我认为这不重要(无论如何我已经尝试将其设置为1秒并且它没有改变任何东西).另外,如果我在LogCat中打印当天的当天和月中的值,它可以正常工作,那么我真的不知道它为什么不更新.请帮帮我!这是我的代码:

public class Henk extends AppWidgetProvider {

AppWidgetManager appWidgetManager;

ComponentName componentName;

RemoteViews remoteViews;

LocationManager locationManager;

@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

// Update the current date

this.appWidgetManager = appWidgetManager;

componentName = new ComponentName(context, Henk.class);

remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

SimpleDateFormat dayofweekformat = new SimpleDateFormat("EEEE");

Date dayofweekdate = new Date(System.currentTimeMillis());

String dayofweeklowercase = dayofweekformat.format(dayofweekdate);

String dayofweek = dayofweeklowercase.toUpperCase();

SimpleDateFormat monthformat = new SimpleDateFormat("MMMM dd, yyyy");

Date monthdate = new Date(System.currentTimeMillis());

String month = monthformat.format(monthdate);

Log.d("TAG", "----> DAY OF WEEK: " + dayofweek); // Fine in LogCat

Log.d("TAG", "----> MONTH AND DATE: " + month); // Fine in LogCat

remoteViews.setTextViewText(R.id.widget_textview1, dayofweek);

remoteViews.setTextViewText(R.id.widget_textview2, month);

appWidgetManager.updateAppWidget(componentName, remoteViews);

}

}

编辑:

我已经实现了Doomsknight提供的解决方案,所以现在我认为我的onUpdate()方法应该没问题.尽管如此,它仍然没有显示我的日期和日期.但是我注意到,当我测试运行它时,onUpdate()实际上是在我的配置活动关闭之前执行的.在我的配置活动中,我有以下代码来初始化我的小部件(至少它应该做什么),我认为错误就在这里:

public void onClick(View view) {

// Launch the Widget and close the configuration Activity

Intent intent2 = new Intent(context, Henk.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);

remoteViews.setOnClickPendingIntent(R.id.configuration_button, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetID, remoteViews);

Log.d("TAG", "----> APP WIDGET ID: " + appWidgetID);

Log.d("TAG", "----> REMOTEVIEWS: " + remoteViews);

Intent result = new Intent();

result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetID);

setResult(RESULT_OK, result);

finish();

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值