Android部件按钮 [英] Android widget button

查看:113
本文介绍了Android部件按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的教程创建窗口小部件。我的问题是按钮。在我的小部件有三行,每包含一个TextView和三个按钮。随着当用户点击ButtonP1,ButtonP2或ButtonP3低于code举杯味精应该看到的不同的消息。现在的问题是,无论我点击该按钮,我得到的第一个敬酒味精每次(消息按钮P1)。

 公共类HelloWidget的扩展AppWidgetProvider {

    公共静态字符串ACTION_WIDGET_CONFIGURE =ConfigureWidget;
    公共静态字符串ACTION_WIDGET_RECEIVER =ActionReceiverWidget;

        @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
        context.startService(新意图(背景下,UpdateService.class));
            意向意图=新的意图(背景下,UpdateService.class);
        context.startService(意向);

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

         意图激活=新的意图(背景下,HelloWidget.class);
         active.setAction(ACTION_WIDGET_RECEIVER);
         active.putExtra(味精,消息按钮P1);

         PendingIntent actionPendingIntent = PendingIntent.getBroadcast(上下文,0,活性,0);
         remoteViews.setOnClickPendingIntent(R.id.ButtonP1,actionPendingIntent);

         意图active2 =新的意图(背景下,HelloWidget.class);
         active2.setAction(ACTION_WIDGET_RECEIVER);
         active2.putExtra(味精,消息巴顿P2);

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

         意图active3 =新的意图(背景下,HelloWidget.class);
         active3.setAction(ACTION_WIDGET_RECEIVER);
         active3.putExtra(味精,消息按钮P3);

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

         appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);

   }

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){

        最后弦乐行动= intent.getAction();
        如果(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(动作)){
        最终诠释appWidgetId = intent.getExtras()调用getInt(
        AppWidgetManager.EXTRA_APPWIDGET_ID,
        AppWidgetManager.INVALID_APPWIDGET_ID);
        如果(appWidgetId!= AppWidgetManager.INVALID_APPWIDGET_ID){
        this.onDeleted(背景下,新的INT [] {appWidgetId});
        }
        } 其他 {
          //检查,如果我们的行动被称为
          如果(intent.getAction()。等于(ACTION_WIDGET_RECEIVER)){
        弦乐味精=空;
        尝试 {
        味精= intent.getStringExtra(味精);
        }赶上(NullPointerException异常E){
        Log.e(错误,味精=空);
        }
        Toast.makeText(背景下,味精,Toast.LENGTH_SHORT).show();
     }
          super.onReceive(背景下,意图);
         }

    }


}
 

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

我在想什么?

解决方案

 意图configIntent4 =新的意图(背景下,Call1.class);
         configIntent4.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent4 = PendingIntent.getActivity(背景下,REQUEST_ code_FOUR,configIntent4,0);
         remoteViews.setOnClickPendingIntent(R.id.Button01,configPendingIntent4);


         意图configIntent5 =新的意图(背景下,Call2.class);
         configIntent5.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent5 = PendingIntent.getActivity(背景下,REQUEST_ code_FIVE,configIntent5,0);
         remoteViews.setOnClickPendingIntent(R.id.Button02,configPendingIntent5);


         意图configIntent6 =新的意图(背景下,Call3.class);
         configIntent6.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent6 = PendingIntent.getActivity(背景下,REQUEST_ code_SIX,configIntent6,0);
         remoteViews.setOnClickPendingIntent(R.id.Button023 configPendingIntent6);
 

解决方案

PendingIntent.getBroadcast(背景下,0,有效,0) 参数: 背景在此PendingIntent应该进行广​​播的语境。 请求code 私人请求,code发件人(目前未使用)。 意向意图播出。 标志

您应该使用不同的请求,code。

i am using this tutorial to create a widget. My problem is with the buttons. In my widget there are three lines, each consists of a textview and three buttons. With the code below when user clicks on ButtonP1, ButtonP2 or ButtonP3 a toast msg should be seen with different messages. The problem is that no matter which button i click, i get the first toast msg every time ("Message for Button P1").

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

    }


}

I don't think the .xml file for the layout is necessary so i am not wasting space with it.

What am i missing?

Solution

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

解决方案

PendingIntent.getBroadcast(context, 0, active, 0) Parameters: context The Context in which this PendingIntent should perform the broadcast. requestCode Private request code for the sender (currently not used). intent The Intent to be broadcast. flags

You should use different requestCode.

这篇关于Android部件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆