ウィジェットがあるので、60秒ごとに更新したい。ウィジェットが最初にホームスクリーンに追加されると、更新機能がうまく動作します。それを超えると、60秒ごとに更新メソッドを再実行するAlarmManagerを起動することになっています。それは実際にやっているようではない部分です。ここに私のコードです:AppWidget alarmmanagerが更新されない
public class ClockWidget extends AppWidgetProvider {
public static String CLOCK_WIDGET_UPDATE = "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE";
@Override
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];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clocklayout);
appWidgetManager.updateAppWidget(appWidgetId, views);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d("log","Entered update cycle");
//Unimportant for these purposes
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private PendingIntent createClockTickIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d("onEnabled","Widget Provider enabled. Starting timer to update widget every minute");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context));
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d("onDisabled", "Widget Provider disabled. Turning off timer");
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockTickIntent(context));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("onReceive", "Received intent " + intent);
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Log.d("onReceive", "Clock update");
// Get the widget manager and ids for this widget provider, then
// call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID: ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
}
}
}
それは私が問題で見つけたいくつかのチュートリアルの産物であり、自分のAndroidの知識です。私のlogcatsによると、Log.d( "onReceive"、 "Clock update")には決して行きません。ライン。そして、はい、私のマニフェストは時計の更新の意図で設定されています。ありがとう!
EDIT:追加情報私は、createClockTickIntentメソッドにログ行を置くと、それが発生します。だから私はこれは、私のアプリケーションがalarmManager.setRepeating行を実行していることを意味しているのではないでしょうか。
スペルミスを修正して自分の問題を解決しましたが、これを投稿してくれてありがとうございます。これは、AlarmManagerを使ってどこでも見たappwidgetを更新する方法の最も簡単で簡単なデモンストレーションです。そこには恐ろしく複雑なソリューションがたくさんありますが、これはすべてを理解し、理解して実装する方法が簡単です。ありがとう! – drmrbrewer