[英]How to set onClickListener on button inside Widget?
I have tried to do according to this solution . 我已尝试根据此解决方案进行操作 。 But it is somehow not working.
但这不起作用。 No errors, which may give clue how to solve.
没有错误,可能会提供解决方法的线索。
Inside my manifest, I have this receiver: 在清单中,我有这个接收者:
<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: 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 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 looks like this: 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);
}
}
When I click Button bRefresh, it should show Toast "Button click" but it is not showing. 当我单击Button bRefresh时,它应显示Toast“ Button click”,但未显示。 How to solve this problem?
如何解决这个问题呢?
inside your on update take, 在更新中,
views.setOnClickPendingIntent(R.id.widget_retry, getPendingSelfIntent(context, MyOnClick));
than take, 比拿
private static final String MyOnClick = "myOnClickTag";
protected PendingIntent getPendingSelfIntent(Context context, String action)
{
// if we brodcast than definetly we have to define ONRECEIVE
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
and, 和,
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (MyOnClick.equals(intent.getAction()))
{
//your onClick action is here
}
}
above code working for me! 以上代码对我有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.