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

后端 未结 4 1970
梦谈多话
梦谈多话 2020-12-13 15:26

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 no

相关标签:
4条回答
  • 2020-12-13 15:51

    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
    }
    
    0 讨论(0)
  • 2020-12-13 15:51

    I would suggest you use an OrientationEventListener object. It does exactly what you are looking for and it is not deprecated. It's been around since API level 3.

    0 讨论(0)
  • 2020-12-13 16:02

    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>
    
    0 讨论(0)
  • 2020-12-13 16:05

    You could create a BroadcastReceiver that listens for Intent.ACTION_CONFIGURATION_CHANGED (android.intent.action.CONFIGURATION_CHANGED)

    Note that it does say:

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

    Which means you can't register your reciever in the manifest file. You would have to register it in code.

    then get the configuration and check what the orientation is as Floern stated.

    0 讨论(0)
提交回复
热议问题