【发布时间】:2014-10-03 11:00:16
【问题描述】:
我目前正在使用三个ImageButtons 创建我的第一个小部件。
我已经非常密切地遵循Clickable widgets in android 描述的答案,但是我无法让它发挥作用。
我已经定义了三个字符串,它们是我的操作:
private String playAction = "playService";
private String stopAction = "stopService";
private String resetAction = "resetService";
接下来在我的OnUpdate() 函数中添加setOnclickPendingIntent:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.widget_play, getPendingSelfIntent(context, playAction));
remoteViews.setOnClickPendingIntent(R.id.widget_pause, getPendingSelfIntent(context, stopAction));
remoteViews.setOnClickPendingIntent(R.id.widget_reset, getPendingSelfIntent(context, resetAction));
manager = appWidgetManager;
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
// Update the widgets via the service
context.startService(intent);
}
getPendingSelfIntent函数定义如下:
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
但是onReceive 函数不接收上述定义的任何操作。我目前记录传入意图的所有动作,但唯一通过的是android.appwidget.action.APPWIDGET_UPDATE 动作。
为了完整起见,这就是我在 Manifest 中定义操作的方式:
<receiver
android:name="MyWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="My Widget">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="playService" />
<action android:name="stopService" />
<action android:name="resetService" />
</intent-filter>
</receiver>
我在 Android 文档中读到,不能使用 setOnClickPendingIntent 函数设置属于集合(ListView 等)的元素。我的布局中有三个按钮。为了确保这不会导致错误,下面是 XML 的样子:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/widget_play"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_marginRight="5dp"
android:src="@drawable/run"/>
<ImageButton
android:id="@+id/widget_pause"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_marginRight="5dp"
android:src="@drawable/pause"/>
<ImageButton
android:id="@+id/widget_reset"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/reset"/>
</LinearLayout>
感谢您对此事的任何帮助,因为我完全没有想法。我已经搜索过类似的问题,但它们都描述了我已经尝试过的内容。
【问题讨论】:
-
发布您的 UpdateWidgetService 类
-
我认为你应该使用
PendingIntent.FLAG_UPDATE_CURRENT作为PendingIntent.getBroadcast(...)的最后一个参数。尝试将return PendingIntent.getBroadcast(context, 0, intent, 0);替换为return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);。
标签: android android-intent android-widget android-pendingintent