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
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 –
vedere http://stackoverflow.com/questions/2748590/clickable-widgets-in-android/2748759 # 2748759 – larham1