Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Please find below an example which does what you ask for, hope this helps.</p> <pre><code>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) ); } } </code></pre> <p>and here comes MyService class</p> <pre><code>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-&gt;" + BCAST_CONFIGCHANGED); if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ // it's Landscape Log.d(TAG, "LANDSCAPE"); } else { Log.d(TAG, "PORTRAIT"); } } } }; } </code></pre> <p>and here is the part to define MyService in manifest file</p> <pre><code>&lt;!-- Services --&gt; &lt;service android:enabled="true" android:name="com.wareninja.android.external.screenorientationlistener.services.MyService"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.CONFIGURATION_CHANGED"/&gt; &lt;/intent-filter&gt; &lt;/service&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload