1. Download our Official Android App: Forums for Android!

Apps Problem with "intent"

Discussion in 'Android Development' started by qw1, May 2, 2011.

  1. qw1

    qw1 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    May 2, 2011

    May 2, 2011
    1
    0
    5
    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:
    Code (Text):
    1.  
    2. public class WidgetProvider extends AppWidgetProvider {
    3.      public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    4.      public static String ACTION_WIDGET_RECEIVER =
    5.  "ActionReceiverWidget";
    6.      @Override
    7.      public void onReceive(Context context, Intent intent) {
    8.          super.onReceive(context, intent);
    9.      }
    10.      @Override
    11.      public void onUpdate(Context context, AppWidgetManager
    12.  appWidgetManager,
    13.              int[] appWidgetIds) {
    14.          RemoteViews views = new RemoteViews(context. getPackageName(),
    15. R.layout.main);
    16.  
    17.         Intent tent = new Intent(context, MainBR.class);
    18.         tent.setAction(ACTION_WIDGET_RECEIVER);
    19.         tent.putExtra("button", "b1");
    20.         PendingIntent ptent = PendingIntent.getBroadcast(context, 0,
    21. tent, 0);
    22.         views.setOnClickPendingIntent(R.id.b1, ptent);
    23.  
    24.         Intent tent2 = new Intent(context, MainBR.class);
    25.         tent2.setAction(ACTION_WIDGET_RECEIVER);
    26.         tent2.putExtra("button", "b0");
    27.         PendingIntent ptent2 = PendingIntent.getBroadcast(context, 0,
    28. tent2, 0);
    29.         views.setOnClickPendingIntent(R.id.b0, ptent2);
    30.  
    31.        appWidgetManager.updateAppWidget(appWidgetIds[0], views);
    32.     }
    33. }
    34.  
    RECEIVER:
    Code (Text):
    1.  
    2. public class MainBR extends BroadcastReceiver {
    3.     /**
    4.      * @see
    5. android.content.BroadcastReceiver#onReceive(Context,Intent)
    6.      */
    7.     @Override
    8.     public void onReceive(Context context, Intent intent) {
    9.         AppWidgetManager appWidgetManager =
    10. AppWidgetManager.getInstance(context);
    11.         RemoteViews remoteViews = new
    12. RemoteViews(context.getPackageName(), R.layout.main);
    13.         ComponentName thisWidget = new ComponentName(context,
    14. WidgetProvider.class);
    15.         final String msg = intent.getStringExtra("button");
    16.         remoteViews.setTextViewText(R.id.tv1, msg);
    17.         appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    18.     }
    19. }
    20.  
    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!
     

    Advertisement

Share This Page

Loading...