Две кнопки виджета Android вызывают одну и ту же активность с разными намерениями

У меня есть виджет homescreenwidget в Android с двумя кнопками. Обе кнопки должны вызывать одну и ту же активность (класс), просто используйте разные намерения плюс дополнительные намерения, чтобы знать, какая кнопка вызывает класс. На данный момент только кнопка1 работает и вызывает активность. Я также получаю ключевое значение в вызываемой активности.

Как я могу заставить работать вторую кнопку? Вот мой код:

             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);

вот 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
задан Cœur 5 March 2017 в 13:17
поделиться