【问题标题】:Two buttons of Android widgets calling same Activity with different intentsAndroid 小部件的两个按钮以不同的意图调用相同的 Activity
【发布时间】:2017-03-05 21:17:45
【问题描述】:

我在 Android 中有一个带有两个按钮的主屏幕小部件。两个按钮都应该调用相同的活动(类),只需使用不同的意图和额外的意图,就可以知道哪个按钮调用了类。 目前只有 button1 正在工作并调用该活动。我还在被调用的 Activity 中收到了 keyvalue。

如何使第二个按钮起作用? 这是我的代码:

             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>

【问题讨论】:

  • 您能否提供有关第二个按钮不起作用的更多详细信息? IE。点击时它什么也没做?
  • 是的,点击后它什么也没做。

标签: android android-widget


【解决方案1】:

@Arnold 你已经创建了 2 个 PendingIntent ......

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

其中 PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) 有 4 个参数。您必须为不同的 PendingIntents 发送不同的“requestCode”。

你的代码应该是....

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

在主类中你需要创建这个...

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

}

使用 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);
    }
}

然后你可以检查是value=1st Button还是value=2nd Button来知道哪个Button被点击了。 它应该可以工作...如果它不起作用,请告诉我是什么问题...

【讨论】:

    【解决方案2】:

    我在小部件上使用 2 个按钮时遇到了类似的问题。我对它们都设置了 OnClickPendingIntents,以不同的 setExtra() 调用来区分。但只有后一个意图被调用,即使在单击第一个按钮时也是如此。 我找到了2个解决方案: - 为两个 PendingIntents 分配不同的操作 - 在第二个 PendingEvent 上设置 PendingEvent.FLAG_CANCEL_CURRENT 标志

    我对 PendingEvents 和 Widgets 比较陌生,所以看不出利弊,会坚持第一种解决方案。

    也许这也可以帮助您解决问题。

    【讨论】:

      【解决方案3】:

      您的小部件将只有 1 个远程视图。尝试将代码 sn-p 的结尾更改为:

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

      否则,在R.layout.maina 中查看您的布局会很有用。

      【讨论】:

      • 我改变了你提到的代码。它也不起作用。 button2 没有任何功能。当您单击它时,该按钮会更改颜色,但它不会启动所需的活动。感谢您的帮助
      • 抱歉阿诺德,除了指出您在 maina.xml 中缺少一个关闭的 &lt;/LinearLayout&gt; 标记外,无法发现问题。但是,我怀疑这是一个复制和粘贴问题,否则 Eclipse 应该将其标记为错误。祝你好运(如果你弄明白了,把答案贴在这里)!
      【解决方案4】:

      就我而言:

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

      在 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>
      

      它正在工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多