0
我使用AlarmManager每秒更新一個部件,但是當添加更多部件時,只有最後一個部件被更新。我不understend爲什麼...Android:報警更新更多部件
這是代碼...
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);
官方建議強烈不鼓勵經常更新,否則電池壽命可能會受到很大影響。最好的祝福。 – pouzzler 2012-04-22 10:57:59