Hello. I have some problem with a programming simple widget. I have 2
buttons (b0, b1) and 1 TextView (tv1). I want that: text in tv1
changes to value, witch is given in "intent.putExtra", on buttons
press. In other words, after b0 pressed, text is "b0", after b1
pressed - b1. But always I have "b1" in tv1. I have no idea, how it
can be.
My code:
RECEIVER:
P.S. I'm sorry for my not good English, it isn't my native language,
and I ask You to write simple, if it is possible. Thank you!
buttons (b0, b1) and 1 TextView (tv1). I want that: text in tv1
changes to value, witch is given in "intent.putExtra", on buttons
press. In other words, after b0 pressed, text is "b0", after b1
pressed - b1. But always I have "b1" in tv1. I have no idea, how it
can be.
My code:
Code:
public class WidgetProvider extends AppWidgetProvider {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER =
"ActionReceiverWidget";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager,
int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context. getPackageName(),
R.layout.main);
Intent tent = new Intent(context, MainBR.class);
tent.setAction(ACTION_WIDGET_RECEIVER);
tent.putExtra("button", "b1");
PendingIntent ptent = PendingIntent.getBroadcast(context, 0,
tent, 0);
views.setOnClickPendingIntent(R.id.b1, ptent);
Intent tent2 = new Intent(context, MainBR.class);
tent2.setAction(ACTION_WIDGET_RECEIVER);
tent2.putExtra("button", "b0");
PendingIntent ptent2 = PendingIntent.getBroadcast(context, 0,
tent2, 0);
views.setOnClickPendingIntent(R.id.b0, ptent2);
appWidgetManager.updateAppWidget(appWidgetIds[0], views);
}
}
Code:
public class MainBR extends BroadcastReceiver {
/**
* @see
android.content.BroadcastReceiver#onReceive(Context,Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager =
AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new
RemoteViews(context.getPackageName(), R.layout.main);
ComponentName thisWidget = new ComponentName(context,
WidgetProvider.class);
final String msg = intent.getStringExtra("button");
remoteViews.setTextViewText(R.id.tv1, msg);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
and I ask You to write simple, if it is possible. Thank you!