其他分享
首页 > 其他分享> > 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();
}

解决方法:

您必须记住,一个应用程序可以有许多小部件.
用户可以将两个或多个放置在屏幕上.

你错过的是通过Ids的循环.您传递了一些您生成的组件ID,而不是小部件ID,因此它不会更新正确的小部件.

小部件只能更新至少30分钟.除非您使用AlarmManager,否则每1秒钟将无法工作.在你的情况下你不需要这个.

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

    for (int appWidgetId : appWidgetIds) {

            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(appWidgetId, remoteViews);
    }
}

标签:android,widget,onupdate
来源: https://codeday.me/bug/20190704/1374179.html