2010-03-18 12 views
17

I saw this topic e implemente IntentService como se describe, pero ¿qué ocurre si quiero más de un botón? ¿Cómo puedo distinguir el botón el uno del otro? Estoy intentando setFlags, pero no puedo leerlo en onHandleIntent) método (:Procesando más de un botón, haga clic en el widget de Android

public static class UpdateService extends IntentService { 
... 

    @Override 
    public void onHandleIntent(Intent intent) { 
     ComponentName me = new ComponentName(this, ExampleProvider.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(me, buildUpdate(this)); 
    } 

    private RemoteViews buildUpdate(Context context) { 
     RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main_layout); 

     Intent i = new Intent(this, ExampleProvider.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     updateViews.setOnClickPendingIntent(R.id.button_refresh, pi); 

     i = new Intent(this, ExampleProvider.class); 
     pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     updateViews.setOnClickPendingIntent(R.id.button_about, pi); 

     return updateViews; 
    } 
} 

En esta pequeña pieza de código que tengo dos PendingIntent vinculados con setOnClickPendingIntent, puedo distinguir esta intención para diferentes acciones y procesamiento? Gracias por la ayuda

+0

parecer , Encontré la respuesta. Debemos registrar el filtro de intención como se describe aquí http://www.helloandroid.com/files/xmaswidget/android_howto-hellowidget.pdf. Ahora intentaré y luego escribiré sobre los resultados. – dive

Respuesta

54

que funciona, fuera de clase Widget que se extiende AppWidgetProvider:

public class ExampleProvider extends AppWidgetProvider { 

// our actions for our buttons 
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh"; 
public static String ACTION_WIDGET_SETTINGS = "ActionReceiverSettings"; 
public static String ACTION_WIDGET_ABOUT = "ActionReceiverAbout"; 


@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_layout); 

    Intent active = new Intent(context, ExampleProvider.class); 
    active.setAction(ACTION_WIDGET_REFRESH); 
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
    remoteViews.setOnClickPendingIntent(R.id.button_refresh, actionPendingIntent); 

    active = new Intent(context, ExampleProvider.class); 
    active.setAction(ACTION_WIDGET_SETTINGS); 
    actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
    remoteViews.setOnClickPendingIntent(R.id.button_settings, actionPendingIntent); 

    active = new Intent(context, ExampleProvider.class); 
    active.setAction(ACTION_WIDGET_ABOUT); 
    actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
    remoteViews.setOnClickPendingIntent(R.id.button_about, actionPendingIntent); 

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) { 
     Log.i("onReceive", ACTION_WIDGET_REFRESH); 
    } else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) { 
     Log.i("onReceive", ACTION_WIDGET_SETTINGS); 
    } else if (intent.getAction().equals(ACTION_WIDGET_ABOUT)) { 
     Log.i("onReceive", ACTION_WIDGET_ABOUT); 
    } else { 
     super.onReceive(context, intent); 
    } 
} 
... 

Y AndroidManifest.xml donde hay que registrarse nuestras acciones:

<receiver android:name="ExampleProvider"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
      <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_REFRESH"/> 
      <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_SETTINGS"/> 
      <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_ABOUT"/> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/> 
    </receiver> 
</application> 

Cuestiones relacionadas