Android小部件的两个按钮以不同的意图调用相同的Activity

时间:2011-10-04 09:42:42

标签: android android-widget

我在Android中有一个带有两个按钮的homescreenwidget。两个按钮都应该调用相同的活动(类)只使用不同的意图和意图附加,以知道哪个按钮称为类。 现在只有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>

4 个答案:

答案 0 :(得分:15)

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

然后,您可以检查值=第一个按钮值=第二个按钮,以了解哪个按钮已被点击。 它应该工作......如果它不起作用请告诉我这是什么问题......

答案 1 :(得分:2)

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

我对PendingEvents和Widgets非常不熟悉,所以我看不出优点和缺点,并坚持第一个解决方案。

也许这可能会帮助你解决问题。

答案 2 :(得分:1)

就我而言:

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>

它在工作。

答案 3 :(得分:0)

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

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中查看您的布局会很有用。