2010-05-01 15 views
10

Rispondo a una pressione di un pulsante sul mio appwidget nel metodo onreceive. Quando ho premuto il pulsante, voglio forzare il widget a chiamare il metodo onupdate. Come posso realizzare questo?Forza il widget Android per aggiornare

Grazie in anticipo!

+0

Credo che sia questo comando: \t \t \t appWidgetManager.updateAppWidget (appWidgetId, visualizzazioni); Ad ogni modo, come si arriva a rispondere a qualcuno che fa clic su di esso? L'ho chiesto in una domanda qui: http://stackoverflow.com/questions/2748590/clickable-widgets-in-android –

+0

vedere http://stackoverflow.com/questions/2748590/clickable-widgets-in-android/2748759 # 2748759 – larham1

risposta

9

Il widget non può effettivamente rispondere ai clic perché non è un processo separato in esecuzione. Ma può avviare il servizio per elaborare il comando:

public class TestWidget extends AppWidgetProvider { 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 

     // Perform this loop procedure for each App Widget that belongs to this provider 
     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 

      // Create an Intent to launch UpdateService 
      Intent intent = new Intent(context, UpdateService.class); 
      PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

      // Get the layout for the App Widget and attach an on-click listener to the button 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout); 
      views.setOnClickPendingIntent(R.id.button, pendingIntent); 

      // Tell the AppWidgetManager to perform an update on the current App Widget 
      appWidgetManager.updateAppWidget(appWidgetId, views); 
     } 
    } 

    public static class UpdateService extends Service { 
     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      //process your click here 
      return START_NOT_STICKY; 
     } 
    } 
} 

Si dovrebbe anche registrare il nuovo servizio nel file manifest:

<service android:name="com.xxx.yyy.TestWidget$UpdateService"> 

È possibile trovare un altro esempio di implementazione UpdateService nel campione Wiktionary in SDK

Ed ecco un altro buon approccio Clickable widgets in android

+0

Il link "altro buon approccio" contraddice la tua affermazione iniziale. I widget possono rispondere ai clic senza che sia necessario un servizio coinvolto utilizzando il gestore onReceive con un intento che viene inviato quando si fa clic sull'elemento del widget. Non avere un servizio coinvolto è una soluzione migliore secondo me. –

+0

onStart ora DEPRECATED –

3

Questo è un pò rozza, ma funziona piuttosto bene per me, come io ho trovato alcun d modo irrealmente implementato per forzare un aggiornamento.

public class Foo extends AppWidgetManager { 
    public static Foo Widget = null; 
    public static Context context; 
    public static AppWidgetManager AWM; 
    public static int IDs[]; 

    public void onUpdate(Context context, AppWidgetManager AWM, int IDs[]) { 
     if (null == context) context = Foo.context; 
     if (null == AWM) AWM = Foo.AWM; 
     if (null == IDs) IDs = Foo.IDs; 

     Foo.Widget = this; 
     Foo.context = context; 
     Foo.AWM = AWM; 
     Foo.IDs = IDs; 
....... 
    } 
} 

Ora, ovunque voglio forzare il widget per l'aggiornamento, è semplice come:

if (null != Foo.Widget) Foo.Widget.onUpdate(null, null, null); 
+0

Se l'utente ha due istanze dei widget, solo uno verrà aggiornato. –

+1

@Kernald Quindi è necessario aver aggiornato solo uno degli ID in 'ID []'. Loop su tutti loro. – Izkata

Problemi correlati