按一下按钮就失去了窗口小部件时屏幕旋转 [英] Button click lost on widget when screen is rotated

查看:102
本文介绍了按一下按钮就失去了窗口小部件时屏幕旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的小工具应用程序,它由一个的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?

下面是code某些环节我使用。

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>

感谢您任何人的帮助!

推荐答案

这帮助我:<一href="http://stackoverflow.com/questions/2653270/android-widget-imagebutton-loses-image-when-screen-is-rotated">Android窗口小部件的ImageButton失去图像时屏幕旋转

在总之,你必须每次调用awm.updateAppWidget之前注册的点击次数(views.setOnClickPendingIntent)

In short, you have to register the clicks (views.setOnClickPendingIntent) before EVERY call to awm.updateAppWidget

这篇关于按一下按钮就失去了窗口小部件时屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆