2011-06-15 65 views
1

我使用this教程来创建小部件。我的问题是按钮。在我的小部件中有三行,每行都包含一个textview和三个按钮。当用户点击ButtonP1,ButtonP2或ButtonP3时,用下面的代码可以看到不同消息的Toast味精。问题是,无论我点击哪个按钮,我每次都会得到第一个祝酒消息(“消息按钮P1”)。Android小部件按钮

public class HelloWidget extends AppWidgetProvider { 

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget"; 
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; 

     @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     context.startService(new Intent(context, UpdateService.class)); 
      Intent intent = new Intent(context, UpdateService.class); 
     context.startService(intent); 

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain); 

     Intent active = new Intent(context, HelloWidget.class); 
     active.setAction(ACTION_WIDGET_RECEIVER); 
     active.putExtra("msg", "Message for Button P1"); 

     PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); 
     remoteViews.setOnClickPendingIntent(R.id.ButtonP1, actionPendingIntent); 

     Intent active2 = new Intent(context, HelloWidget.class); 
     active2.setAction(ACTION_WIDGET_RECEIVER); 
     active2.putExtra("msg", "Message for Button P2"); 

     PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 0, active2, 0); 
     remoteViews.setOnClickPendingIntent(R.id.ButtonP2, actionPendingIntent2); 

     Intent active3 = new Intent(context, HelloWidget.class); 
     active3.setAction(ACTION_WIDGET_RECEIVER); 
     active3.putExtra("msg", "Message for Button P3"); 

     PendingIntent actionPendingIntent3 = PendingIntent.getBroadcast(context, 0, active3, 0); 
     remoteViews.setOnClickPendingIntent(R.id.ButtonP3, actionPendingIntent3); 

     appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 

    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     final String action = intent.getAction(); 
     if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { 
     final int appWidgetId = intent.getExtras().getInt(
     AppWidgetManager.EXTRA_APPWIDGET_ID, 
     AppWidgetManager.INVALID_APPWIDGET_ID); 
     if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { 
     this.onDeleted(context, new int[] { appWidgetId }); 
     } 
     } else { 
      // check, if our Action was called 
      if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
     String msg = "null"; 
     try { 
     msg = intent.getStringExtra("msg"); 
     } catch (NullPointerException e) { 
     Log.e("Error", "msg = null"); 
     } 
     Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); 
    } 
      super.onReceive(context, intent); 
     } 

    } 


} 

我不认为布局的.xml文件是必要的,所以我不会浪费空间。

我在想什么?

Intent configIntent4 = new Intent(context, Call1.class); 
     configIntent4.setAction(ACTION_WIDGET_CONFIGURE); 
     PendingIntent configPendingIntent4 = PendingIntent.getActivity(context, REQUEST_CODE_FOUR, configIntent4, 0); 
     remoteViews.setOnClickPendingIntent(R.id.Button01, configPendingIntent4); 


     Intent configIntent5 = new Intent(context, Call2.class); 
     configIntent5.setAction(ACTION_WIDGET_CONFIGURE); 
     PendingIntent configPendingIntent5 = PendingIntent.getActivity(context, REQUEST_CODE_FIVE, configIntent5, 0); 
     remoteViews.setOnClickPendingIntent(R.id.Button02, configPendingIntent5); 


     Intent configIntent6 = new Intent(context, Call3.class); 
     configIntent6.setAction(ACTION_WIDGET_CONFIGURE); 
     PendingIntent configPendingIntent6 = PendingIntent.getActivity(context, REQUEST_CODE_SIX, configIntent6, 0); 
     remoteViews.setOnClickPendingIntent(R.id.Button023 configPendingIntent6); 

回答

1

PendingIntent.getBroadcast(上下文,0,活性,0) 参数: 上下文在此的PendingIntent应当执行广播的上下文。 requestCode发件人的私人请求代码(当前未使用)。 intent意向被广播。 flags

您应该使用不同的requestCode。

+0

我已经解决了它,但忘了添加解决方案。这次我更新了我的问题。不管怎么说,还是要谢谢你。 – erdomester 2011-07-11 05:04:27