Android widget button(Android 小部件按钮)
问题描述
我正在使用 this 教程来创建一个小部件.我的问题是按钮.在我的小部件中有三行,每行由一个文本视图和三个按钮组成.使用下面的代码,当用户单击 ButtonP1、ButtonP2 或 ButtonP3 时,应该会看到带有不同消息的 toast msg.问题是无论我点击哪个按钮,我每次都会收到第一个 toast 消息(按钮 P1 的消息").
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);
}
}
}
我认为布局的 .xml 文件不是必需的,所以我不会浪费空间.
I don't think the .xml file for the layout is necessary so i am not wasting space with it.
我错过了什么?
解决方案
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)参数:context 这个 PendingIntent 应该在其中执行广播的 Context.requestCode 发件人的私人请求代码(当前未使用).intent 要广播的 Intent.标志
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
你应该使用不同的 requestCode.
You should use different requestCode.
这篇关于Android 小部件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 小部件按钮
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01