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));
}
Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });
In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.
int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).
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>
You probably shouldn't need to do this. You can use layout and layout-land folders to provide different layouts in each orientation via xml without manually detecting rotation. However, you shouldn't do anything functionally different in each orientation as a lot of devices can't rotate the homescreen and whatever features you put in will be inaccessible for them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With