【发布时间】:2015-07-15 12:19:18
【问题描述】:
我已经尝试按照这个solution去做。但它在某种程度上不起作用。 没有错误,这可能会提供解决方法的线索。
在我的清单中,我有这个接收器:
<receiver android:name=".ImageReceiver">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_image" />
</receiver>
widget_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget_layout">
</appwidget-provider>
widget_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivImage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/bRefresh" />
</RelativeLayout>
ImageReceiver 看起来像这样:
public class ImageReceiver extends AppWidgetProvider {
public static final String REFRESH_BUTTON_CLICKED = "REFRESH_BUTTON_CLICKED";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.bRefresh, getPendingSelfIntent(context, REFRESH_BUTTON_CLICKED));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (REFRESH_BUTTON_CLICKED.equals(intent.getAction())) {
Toast.makeText(context, "Button click", Toast.LENGTH_LONG).show();
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
当我单击按钮 bRefresh 时,它应该显示 Toast“按钮单击”,但它没有显示。如何解决这个问题?
【问题讨论】:
-
尝试返回
PendingIntent.getActivity(context, 0, intent, 0); -
@MD,没有效果。我还能做什么?