我使用AlarmManager每秒更新一个小部件,但是当添加更多小部件时,只更新最后一个小部件。我不知道为什么......
这是代码......
public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
SharedPreferences preference = context.getSharedPreferences("MY_PREFERENCE", Context.MODE_PRIVATE);
Intent intent = new Intent(context,vampireServiceWidget.class);
context.startService(intent);
for(int i=0; i<appWidgetIds.length;i++){
if(preference.getLong("countdown", 0)!=0){
//Parte il countdown
Intent in = new Intent().setAction("it.android.dev.thevampirediaries.UPDATE_COUNTDOWN");
in.putExtra("id_widget", appWidgetIds[i]);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 1000, pi);
} }
}
进入播放此代码
AppWidgetManager manager = AppWidgetManager.getInstance(context);
RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.widgett);
thisWidget=intent.getIntExtra("id_widget", 0)
SimpleTimeFormat tf = new SimpleTimeFormat("$dd$ : $HH$: $mm$: $ss$");
String risultato = tf.format(countdown_data_fine - new Date().getTime()); // arg0 tempo
remoteView.setTextViewText(R.id.textView1, titolo_puntata);
remoteView.setTextViewText(R.id.textView2, risultato);
manager.updateAppWidget(thisWidget, remoteView);
答案 0 :(得分:0)
注释掉for循环,不要发送小部件ID。使用public void updateAppWidget (ComponentName provider, RemoteViews views) to update the widget。
试试这个
public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
SharedPreferences preference = context.getSharedPreferences("MY_PREFERENCE", Context.MODE_PRIVATE);
Intent intent = new Intent(context,vampireServiceWidget.class);
context.startService(intent);
//for(int i=0; i<appWidgetIds.length;i++){
if(preference.getLong("countdown", 0)!=0){
//Parte il countdown
Intent in = new Intent().setAction("it.android.dev.thevampirediaries.UPDATE_COUNTDOWN");
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 1000, pi);
}
}
//}
=============================================== =
AppWidgetManager manager = AppWidgetManager.getInstance(context);
RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.widgett);
ComponentName thisWidget = new ComponentName(context, widget.class);
SimpleTimeFormat tf = new SimpleTimeFormat("$dd$ : $HH$: $mm$: $ss$");
String risultato = tf.format(countdown_data_fine - new Date().getTime());
remoteView.setTextViewText(R.id.textView1, titolo_puntata);
remoteView.setTextViewText(R.id.textView2, risultato);
manager.updateAppWidget(thisWidget, remoteView);