4

私はAndroid用のウィジェットに取り組んでいます。すべきことの 1 つは、現在の曜日と月の日付を表示することです。私のコードは大丈夫だと思いますが、何らかの理由で更新されません。私のプロバイダーの更新期間は 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();
}
4

1 に答える 1

2

1つのアプリケーションに多くのウィジェットが存在する可能性があることに注意する必要があります。ユーザーは2つ以上を画面に配置できます。

あなたが見逃したのは、IDをループすることです。ウィジェットIDではなく生成したコンポーネントIDを渡したため、正しいウィジェットが更新されません。

ウィジェットは最低30分しか更新できません。を使用しない限り、1秒ごとは機能しませんAlarmManager。あなたの場合、これは必要ありません。

@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);
    }
}
于 2012-09-13T14:07:39.730 に答える