如何检测的方向变化,主屏幕小工具? [英] How to detect orientation change in home screen widget?

查看:103
本文介绍了如何检测的方向变化,主屏幕小工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个主屏幕小部件,并要更新的主屏幕小部件当设备方向的变化,从纵向到横向或其他方式。我如何能做到吗?

目前,我试图reigster到CONFIGURATION_CHANGED行动像下面的code,但在Android却不允许我这样做,说IntentReceiver组件不允许注册以接收意图。有人可以帮我吗?谢谢你。

 公共类进myWidget扩展AppWidgetProvider {

    @覆盖
    公共无效onEnabled(上下文的背景下){
        super.onEnabled(上下文);
        this.registerReceiver(这一点,新的IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
    }
 

解决方案

我知道这是一个老问题,但有确实是一个更简单的方法比委托更新到IntentService做到这一点。 应对方向改变的方式是听在应用程序配置更改和广播的ACTION_APPWIDGET_UPDATE的部件。

添加应用程序类,您的应用程序

 公共类MyApplication的扩展应用{

    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);

        //创建意图更新窗口小部件的所有实例
        意向意图=新的意图(AppWidgetManager.ACTION_APPWIDGET_UPDATE,空,这一点,MyWidget.class);

        //检索所有appWidgetIds小部件和放大器;把它放入意向
        AppWidgetManager appWidgetMgr = AppWidgetManager.getInstance(本);
        组件名厘米=新的组件名(本,MyWidget.class);
        INT [] appWidgetIds = appWidgetMgr.getAppWidgetIds(厘米);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);

        //更新小部件
        sendBroadcast(意向);
    }
}
 

每当配置发生变化的方法将被调用,其中之一是一个方向变化。你可以通过做广播改进方法,只有当它是一个方向变化,而不是如系统语言的改变。 该方法主要发送更新广播给小部件的所有实例。

定义所有MyApplication在你的清单

 <应用
    机器人:名称=yourpackagename.MyApplication
    机器人:说明=@字符串/ APP_NAME
    机器人:标签=@字符串/ APP_NAME
    机器人:图标=@可绘制/ app_icon>

    <! - 在这里走你的活动定义 - >

< /用途>
 

I am writing a home screen widget and want to update the home screen widget when the device orientation changes from portrait to landscape or the other way. How can I make it?

Currently, I tried to reigster to CONFIGURATION_CHANGED action like the code below, but the Android didn't allow me to do that by saying "IntentReceiver components are not allowed to register to receive intents". Can someone help me? Thanks.

public class MyWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        this.registerReceiver(this, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
    }

解决方案

I know it's an old question but there's indeed an easier way to do this than delegating the update to an IntentService. The way to deal with orientation changes is to listen for configuration changes in the Application and broadcast an ACTION_APPWIDGET_UPDATE to the widget.

Add an Application class to your app

public class MyApplication extends Application {

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // create intent to update all instances of the widget
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidget.class);

        // retrieve all appWidgetIds for the widget & put it into the Intent
        AppWidgetManager appWidgetMgr = AppWidgetManager.getInstance(this);
        ComponentName cm = new ComponentName(this, MyWidget.class);
        int[] appWidgetIds = appWidgetMgr.getAppWidgetIds(cm);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

        // update the widget
        sendBroadcast(intent);
    }
}

The method will be called whenever a configuration change occurs, one of them being an orientation change. You could improve the method by doing the broadcast only when it's an orientation change and not e.g. a change of the system language. The method basically sends an update broadcast to all instances of your widget.

Define the MyApplication in your manifest

<application
    android:name="yourpackagename.MyApplication"
    android:description="@string/app_name"
    android:label="@string/app_name"
    android:icon="@drawable/app_icon">

    <!-- here go your Activity definitions -->

</application>

这篇关于如何检测的方向变化,主屏幕小工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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