views:

318

answers:

1

I want my widget to update when the phones orientation has changed. Before android 2.0 you could register your widget to get the intent on orientation change

<intent-filter>
   <action android:name="android.intent.action.CONFIGURATION_CHANGED" />
</intent-filter>

but after 2.0 you cannot do it. Android Dev doc says:

You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().

Good advice to try to register it, but you can not register a receiver in a AppWidgetProvider.

I just want to know when the phone switches orientation so I can show some text correctly to the user when I run low on vertical space.

A: 

You can create a service and register it to the CONFIGURATION_CHANGED event, like this

public class MyService extends Service {


@Override
public void onCreate() {
    super.onCreate();

    BroadcastReceiver bReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            refreshWidget();  // Code to refresh the widget
        }
    };

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);
    registerReceiver(bReceiver, intentFilter);

`}

frusso