Skip to content Skip to sidebar Skip to footer

Android : Alarm For Update More Widget

I use an AlarmManager for update every second a widget but when add more widget only the last widget is updated. I don't understend why... This is the code... public class widget e

Solution 1:

Comment out the for loop and do not send the widget ids. Use public void updateAppWidget (ComponentName provider, RemoteViews views) to update the widget.

Try this

publicclasswidgetextendsAppWidgetProvider {

    @OverridepublicvoidonUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
        SharedPreferencespreference= context.getSharedPreferences("MY_PREFERENCE", Context.MODE_PRIVATE);
        Intentintent=newIntent(context,vampireServiceWidget.class);
        context.startService(intent);
        //for(int i=0; i<appWidgetIds.length;i++){        if(preference.getLong("countdown", 0)!=0){
                //Parte il countdownIntentin=newIntent().setAction("it.android.dev.thevampirediaries.UPDATE_COUNTDOWN");
                in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntentpi= PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManageralarm= (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);

Post a Comment for "Android : Alarm For Update More Widget"