2012-05-14 13 views
10

Ho lavorato su un widget e ho fatto alcuni progressi (spero) ma non riesco ancora a farlo fare quello che voglio.Il widget Android non si aggiorna - ha l'ID del widget errato

Ho un'attività di configurazione che funziona bene. Quando si chiude, chiamo il metodo updateAppWidget nella classe WordWidget e il widget aggiorna il numero casuale.

Quello che non posso fare è ottenere il numero casuale da cambiare a causa di onClick. Secondo il registro, inserisco il metodo onReceive(), ma questo è tutto.

Nel registro sto stampando l'ID del widget. Ho notato che quando si esegue dall'attività del configuratore è stampando appWidgetId = 33, ma quando premo il widget stampa appWidgetId = 24.

Questo è il codice:

public class WordWidget extends AppWidgetProvider 
{ 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     //This is run when a new widget is added or when the update period expires. 
     Log.v("wotd", "In onUpdate(), updating " + appWidgetIds.length + " widgets"); 
     Log.v("wotd", " the array is " + appWidgetIds.toString()); 

     for(int x = 0; x < appWidgetIds.length; x++) 
     { 
      Integer appWidgetId = appWidgetIds[x]; 


      //Method that updates the random value 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
     } 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     super.onReceive(context, intent); 
     Log.v("wotd", "In onReceive() with intent=" + intent.toString()); 
     if (intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")) 
     { 
      Integer mAppWidgetId; 
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

      Bundle extras = intent.getExtras(); 
      if(extras != null) 
      { 
       Log.v("wotd", "In onReceive(), extras is valid"); 
       mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 

       //If they gave us an intent without a valid widget Id, just bail. 
       if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) 
       { 
        Log.v("wotd", "In onReceive(), mAppWidgetId is ok"); 
        updateAppWidget(context, appWidgetManager, mAppWidgetId); 
       } 
      } else 
      { 
       Log.v("wotd", "In onReceive(), extras is INVALID"); 
       mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; 
      } 
     } 


    } 


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, Integer appWidgetId) 
    { 
     Log.v("wotd", "In updateAppWidget() with appWidgetId=" + appWidgetId); 

     //Setup onClick 
     Intent widgetIntent = new Intent(context, WordWidget.class); 
     widgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE"); 
     widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
     PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0); 

     Integer nextNumb = new Random().nextInt(100); 
     Log.v("wotd", "In updateAppWidget(), nextNumb = " + nextNumb.toString()); 

     RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
     remoteView.setTextViewText(R.id.mainText, String.valueOf(nextNumb)); 
     remoteView.setOnClickPendingIntent(R.id.mainText, widgetPendingIntent); 

     // Tell the widget manager 
     appWidgetManager.updateAppWidget(appWidgetId, remoteView); 
    } 
} 

Questo è il file di registro, da quando tocco il widget (si noti che l'attività di preferenze ha anche una stampa di registro e mostra l'ID widget come un numero diverso che questo):

05-14 17:15:47.453: V/wotd(1585): In onReceive() with intent=Intent { act=android.appwidget.action.APPWIDGET_UPDATE flg=0x10000000 cmp=com.rfxlabs.wordofthedaywidget/.WordWidget bnds=[202,177][222,201] (has extras) } 
05-14 17:15:47.453: V/wotd(1585): In onReceive(), extras is valid 
05-14 17:15:47.463: V/wotd(1585): In onReceive(), mAppWidgetId is ok 
05-14 17:15:47.463: V/wotd(1585): In updateAppWidget() with appWidgetId=24 

05-14 17: 15: 47,463: V/WOTD (1585): In updateAppWidget(), nextNumb = 42

Quindi, come potete vedere, io sono in realtà entrando nel mio updateAppWidget metodo, ma per qualche motivo l'applicazioneWidgetId non è corretta.

Qualche idea del perché questo sta accadendo? Molte grazie!

+0

ciò che è problema che si vuole per modificare i tuoi widget sul widget di cick o qualcosa di diverso? –

+0

Sì, desidero cambiare il numero casuale ogni volta che tocco il widget. – Sandy

+0

Sei sicuro di non avere due copie dello stesso codice con una leggera differenza .. – Ronnie

risposta

20

Nella funzione updateAppWidget vai a questa linea:

PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0); 

... e trasformarla in:

PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, widgetIntent, 0); 
+0

javadocs dichiara esplicitamente che il secondo parametro (codice richiesta) non viene utilizzato. –

+0

Funziona! Non so perché. Qualcuno può spiegare qual è la magia che accade in questa linea? – deviant

+3

Non c'è magia, solo PendingIntents deve essere univoco in base alla progettazione. Gli ID widget utilizzati come codici di richiesta aiutano a differenziare un'istanza da un'altra –

0

per aggiornare il widget di schermata iniziale sul touch è necessario registrare una trasmissione su misura ricevitore in Manifest e aggiungerlo come azione con widget come:

Passaggio 1: Registrare Ricevitore personalizzato come:

<intent-filter> 
<action android:name="com.imrankhanandroid.HomeWidgetRandomNum.ACTION_WIDGET_RANDOM"/> 
</intent-filter> 

Fase 2: Nel tuo widget Classe AddAction al layout:

public void onReceive(Context paramContext, Intent paramIntent) 
    { 
     //Intent 
     String str = paramIntent.getAction(); 
     if (paramIntent.getAction().equals(ACTION_WIDGET_RANDOM)) { 
      updateWidgetState(paramContext, str); 
     } 
     else 
      { 
       super.onReceive(paramContext, paramIntent); 
      } 
    } 

FASE 3: In Your updateWidgetState aggiungere nuove azioni per Intent come:

rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain); 
Intent active = new Intent(paramContext, Randomnuberwidget.class); 
active.setAction(ACTION_WIDGET_RANDOM); 
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0); 
rview.setOnClickPendingIntent(R.id.ImageView_gps, actionPendingIntent); 

Finalmente puoi scaricare l'esempio Working HomeWidget da QUI HomeWidgetRendomNumber Which Gen erating Random Number On Every Click.

0

È necessario impostare un ultimo bandiera in un getBroadcast: PendingIntent.FLAG_UPDATE_CURRENT

Problemi correlati