Two buttons of Android widgets causing the same activity with different intentions

I have a home widget in Android with two buttons. Both buttons should trigger the same action (class), just use different intentions and intentions to find out which button is called a class. So far, only button1 works and triggers an action. I also get the key value in the called activity.

How to make the second button work? Here is my code:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for ( int i =0; i<appWidgetIds.length ; i++){ int appWidgetId = appWidgetIds[i]; Intent intent2 = new Intent(context, Main.class); Intent intent1 = new Intent(context, Main.class); // Intent put extras Button 1 String bread1 = "secure"; Bundle basket1 = new Bundle(); basket1.putString("key", bread1); intent1.putExtras(basket1); // Intent put extras Button 2 String bread2 = "insecure"; Bundle basket2 = new Bundle(); basket2.putString("key", bread2); intent2.putExtras(basket2); PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0); PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0); RemoteViews views1 = new RemoteViews(context.getPackageName(),R.layout.maina); RemoteViews views2 = new RemoteViews(context.getPackageName(),R.layout.maina); views1.setOnClickPendingIntent(R.id.button1, pending1); views2.setOnClickPendingIntent(R.id.button2, pending2); appWidgetManager.updateAppWidget(appWidgetId, views1); appWidgetManager.updateAppWidget(appWidgetId, views2); 

here is maina.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/tvWidget" android:textAppearance="?android:attr/textAppearanceLarge"></TextView> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="horizontal"> <Button android:text="@string/button1" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button> <Button android:text="@string/button2" android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button> </LinearLayout> </LinearLayout> 
+6
source share
4 answers

@Arnold, you created 2 PendingIntent which ...

 PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0); PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0); 

Where PendingIntent.getActivity (context context, int requestCode, Intent intent, int flags) has 4 parameters. You must send different " requestCode " for different PendingIntents.

Your code should be ....

 PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pending2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); 

in the main class you need to create this ...

 String value; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Bundle b=intent.getExtras(); try{ value=b.getString("key"); } catch (Exception e) { // TODO: handle exception } super.onReceive(context, intent); } 

with code onUpdate ....

  @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Get all ids ComponentName thisWidget = new ComponentName(context, main.class); int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); for (int widgetId : allWidgetIds) { // Create some random data RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); // Register an onClickListener for 1st button Intent intent = new Intent(context, main.class); intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds); intent.putExtra("key", "1st Button"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent); // Register an onClickListener for 2nd button............ Intent intent2 = new Intent(context, main.class); intent2.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds); intent2.putExtra("key", "2nd Button"); PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2); appWidgetManager.updateAppWidget(widgetId, remoteViews); } } 

Then you can check if value = 1st Button or value = 2nd Button to find out which button was pressed. It should work ... IF this does not work, please let me know what the problem is ...

+12
source

I had a similar problem using 2 buttons on widgets. I installed OnClickPendingIntents on both of them, which differ in different calls to setExtra (). But only the last intention was triggered even when the first button was pressed. I found 2 solutions: - assign different actions for both PendingIntents - on the second PendingEvent, set the PendingEvent.FLAG_CANCEL_CURRENT flag

I am not familiar with PendingEvents and Widgets, so I do not see the pros and cons and stick to the first solution.

Perhaps this will also help you with your problem.

+2
source

In my case:

 remoteViews.setOnClickPendingIntent(R.id.widget_head, touch_man(context)); 

remoteViews.setOnClickPendingIntent (R.id.widget_head2, touch_woman (context));

 public static PendingIntent touch_man(Context context) { Intent intent = new Intent(); intent.setAction("touch_man"); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } public static PendingIntent touch_woman(Context context) { Intent intent = new Intent(); intent.setAction("touch_woman"); return PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); } 

In AndroidManifest

 <receiver android:name="Receiver" android:label="widgetBroadcastReceiver" > <intent-filter> <action android:name="touch_man" /> <action android:name="touch_woman"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/demo_widget_provider" /> </receiver> 

Works.

+1
source

Your widget will have only 1 remote view. Try changing the end of the code snippet to:

 RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.maina); remoteView.setOnClickPendingIntent(R.id.button1, pending1); remoteView.setOnClickPendingIntent(R.id.button2, pending2); appWidgetManager.updateAppWidget(appWidgetId, remoteView); 

Otherwise, it would be useful to see your layout in R.layout.maina .

0
source

Source: https://habr.com/ru/post/898576/


All Articles