Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a service to monitor Orientation change in Android

Tags:

I'm writing a Widget that will display a countdown timer. I have the widget working the way I want it until I flip the phone from landscape to portrait. My widget does not update and goes to it's initial state at start of the widget until an onupdate is called by my recurring alarm. I would like to call an onupdate manually once the orientation changes to update my widget I've been researching this for a while now and I've found out that I need to use a Service which will monitor the orientation changes and call my onupdate for my widget.

My problem is I can't find a straight answer as to how to use a service to monitor the change. I've seen that with an activity I can add android:configChanges="orientation|keyboardHidden" to the manifest for an activity and use a onConfigurationChanged, but can I do this for a service. If so how? Is there a better way to monitor the orientation change? I've also read on the internet that a service isn't the best way to do this either.

I've seen tutorials for creating orientation listeners but they seem to use depreciated function calls.

Thanks in Advance

like image 946
Dysen Avatar asked Jan 07 '11 11:01

Dysen


People also ask

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

How do I manage different orientations in Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

Which method is called when screen changes orientation in Android?

First onPause(), onStop() and onDestroy() will be called simultaneously and after some time when the activity restarts the onCreate(), onStart() and onReume() methods will be called simultaneously. So when you change the orientation of a screen the Activity will be restarted.

How can we detect the orientation of the screen?

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 });


2 Answers

Service#onConfigurationChanged(Configuration newConfig) works for me. No need to register Receivers for my opinion. I also did not add any filter to AndroidManifest. Did I miss something in the discussion since nobody suggested this solution? My Service is running on foreground.

Just place inside your service:

@Override public void onConfigurationChanged(Configuration newConfig) {     super.onConfigurationChanged(newConfig);     //Your handling } 
like image 129
OneWorld Avatar answered Sep 30 '22 16:09

OneWorld


Please find below an example which does what you ask for, hope this helps.

public class ScreenOrientationListener extends Activity {      private static final String TAG = "ScreenOrientationListener";      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          mContext = this;          setContentView(R.layout.main);          startService( new Intent(this, MyService.class) );     } } 

and here comes MyService class

public class MyService extends Service {     private static final String TAG = "MyService";      private static final String BCAST_CONFIGCHANGED = "android.intent.action.CONFIGURATION_CHANGED";     private static Context mContext;      @Override     public IBinder onBind(Intent intent) {         return null;     }      @Override     public void onCreate() {         Log.d(TAG, "onCreate()");          mContext = this;          IntentFilter filter = new IntentFilter();         filter.addAction(BCAST_CONFIGCHANGED);         this.registerReceiver(mBroadcastReceiver, filter);     }      @Override     public void onDestroy() {                    Log.d(TAG, "onDestroy()");         //Unregister receiver to avoid memory leaks         mContext.unregisterReceiver(mBroadcastReceiver);     }      @Override     public void onStart(Intent intent, int startid) {         Log.d(TAG, "onStart()");     }      public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {         @Override         public void onReceive(Context context, Intent myIntent) {              if ( myIntent.getAction().equals( BCAST_CONFIGCHANGED ) ) {                  Log.d(TAG, "received->" + BCAST_CONFIGCHANGED);                   if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){                     // it's Landscape                     Log.d(TAG, "LANDSCAPE");                 }                 else {                     Log.d(TAG, "PORTRAIT");                 }             }         }     }; } 

and here is the part to define MyService in manifest file

<!-- Services -->         <service android:enabled="true"  android:name="com.wareninja.android.external.screenorientationlistener.services.MyService">             <intent-filter>                 <action android:name="android.intent.action.CONFIGURATION_CHANGED"/>             </intent-filter>         </service> 
like image 44
Yilmaz Guleryuz Avatar answered Sep 30 '22 16:09

Yilmaz Guleryuz