我要更改布尔变量值以及单击它时小部件的背景吗? 这是小部件xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f15"
android:padding="@dimen/widget_margin"
android:id="@+id/appwidget_layout">
<TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:textColor="#ffffff"
android:textSize="24sp"
android:textStyle="bold|italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/appwidget_button"/>
这是小部件java的用法
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_app_widget_provider);
Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent buttonPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.appwidget_button, buttonPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
布尔布尔公共按钮状态= false;
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_app_widget_provider);
if (intent.getAction()!= null) {
Bundle extras = intent.getExtras();
if (buttonState) {
buttonState = false;
views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.GREEN);
views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.GREEN);
Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
} else {
buttonState = true;
views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.RED);
views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.RED);
Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context, ExampleAppWidgetProvider.class),
views);
}
}
}
代码运行时,不会执行onRecieve方法中的部分。我想知道如何从发送该意图的同一小部件接收点击,以便我可以对其进行修改。
预先感谢
答案 0 :(得分:0)
在您的Widget
类的顶部,创建一个静态变量,它将是您的onClick
:
private static final String MyOnClick = "myOnClickTag";
定义一个帮助程序方法以自动创建每个PendingIntent
:
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
将此onClick
标记设置为您的视图,如下所示:
remoteViews.setOnClickPendingIntent(R.id.button,
getPendingSelfIntent(context, MyOnClick));
在您的onReceive
类中创建一个Widget
方法,并在其中设置此onClick
事件:
public void onReceive(Context context, Intent intent) {
if (MyOnClick.equals(intent.getAction())){
Bundle extras = intent.getExtras();
if (buttonState) {
buttonState = false;
views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.GREEN);
views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.GREEN);
Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
} else {
buttonState = true;
views.setInt(R.id.appwidget_layout, "setBackgroundColor", Color.RED);
views.setInt(R.id.appwidget_text, "setBackgroundColor", Color.RED);
Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context, ExampleAppWidgetProvider.class),
views);
}
};
每当按下设置标签的视图时,onReceive
都会捕获该标签,并执行与我们日常的标准onClick
事件相同的操作
关于您的onUpdate
:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_det);
thisWidget = new ComponentName(context, MyWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.widget_button_stayarm, getPendingSelfIntent(context, MyOnClick1));
remoteViews.setOnClickPendingIntent(R.id.widget_button_awayarm, getPendingSelfIntent(context, MyOnClick2));
remoteViews.setOnClickPendingIntent(R.id.widget_button_dissarm, getPendingSelfIntent(context, MyOnClick3));
remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "gps cords");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}