2011-10-04 33 views
6

我有一个Android的homescreenwidget有两个按钮。两个按钮都应该调用相同的活动(类),只需使用不同的意图和意图附加功能,即可知道哪个按钮称为类。 现在只有button1正在工作并调用活动。我还会在被调用的活动中收到关键值。Android小部件的两个按钮调用相同的活动有不同的意图

我怎样才能使第二个按钮的工作? 这里是我的代码:

   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> 
+0

你能否提供关于第二个按钮不起作用的更多详细信息?即点击时它什么都不做? –

+0

对,它在点击时根本没有做任何事情。 – Arnold

回答

12

@Arnold已创建2的PendingIntent这是....

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

凡PendingIntent.getActivity(上下文的背景下,INT requestCode,意图意图,诠释标志)有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 Buttonvalu e =第二个按钮了解哪个Button已被点击。 它应该工作...如果它不能正常工作,请让我知道是什么问题...

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有用。

+0

我改变了你提到的代码。它不工作。 button2没有功能。该按钮会在您点击它时更改颜色,但不会启动所需的活动。感谢您的帮助 – Arnold

+0

对不起,Arnold除了指出您在maina.xml中缺少一个关闭的标签之外,不能发现问题。但是,我怀疑这是一个复制和粘贴问题,因为Eclipse应该将此标记为错误,否则。祝你好运(如果你想出来的话,在这里发布答案)! –

2

我在使用widget上的2个按钮时遇到了类似的问题。我在它们上设置了OnClickPendingIntents,并通过不同的setExtra()调用进行区分。但只有后者的意图被称为,即使点击第一个按钮。 我发现2个解决方案: - 分配不同的行动,既PendingIntents - 第二PendingEvent设置PendingEvent.FLAG_CANCEL_CURRENT标志

我与PendingEvents和窗口小部件很陌生,所以我不能看到利弊,将坚持第一个解决方案。

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

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> 

它的工作。

相关问题