Button click lost on widget when screen is rotated(屏幕旋转时,小部件上的按钮单击丢失)
问题描述
我有一个非常简单的小部件应用程序,它由一个带有背景的 LinearLayout
和一个 ImageButton
组成.
I have a very simple widget application which consists of a LinearLayout
with a background and an ImageButton
.
在 AppWidgetProvider
onUpdate()
方法中,我注册了单击按钮以广播意图.当小部件第一次加载时,一切都运行良好并且点击被捕获.当屏幕旋转时出现问题,即使屏幕旋转回来也不会再次捕获点击.
In the AppWidgetProvider
onUpdate()
method, I register the click of the button to broadcast an intent. When the widget first loads, everything runs fine and the click is captured. The problem occurs when the screen is rotated, and the click is never captured again even if the screen is rotated back.
屏幕旋转时我需要做什么才能重新注册点击?
What do I have to do to re-register the click when the screen rotates?
下面是我正在使用的一些代码段.
below is some segments of code I am using.
AppWidgetProvider
AppWidgetProvider
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if(intent.getAction().equals("test.CLICK"))
{
CallTestMethod(context);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.widget);
Intent clickintent=new Intent("test.CLICK");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.change_mode, pendingIntentClick);
SetInitialLayout(context);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
清单
<receiver android:name=".Widget" android:label="@string/widget_name">
<intent-filter>
<action android:name="android.appwidget.action.ACTION_APPWIDGET_CONFIGURE" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="test.CLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_mode_switcher" />
</receiver>
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="140dip"
android:layout_height="140dip"
android:scaleType="fitCenter"
android:orientation="vertical"
android:background="@drawable/background">
<ImageButton
android:id="@+id/change_mode"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:orientation="vertical"
android:src="@drawable/none_selected"
android:background="@null"
android:clickable="true" />
</LinearLayout>
感谢大家的帮助!
推荐答案
这对我有帮助:Android小部件ImageButton在屏幕旋转时丢失图像
简而言之,您必须在每次调用 awm.updateAppWidget 之前注册点击次数 (views.setOnClickPendingIntent)
In short, you have to register the clicks (views.setOnClickPendingIntent) before EVERY call to awm.updateAppWidget
这篇关于屏幕旋转时,小部件上的按钮单击丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:屏幕旋转时,小部件上的按钮单击丢失


基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01