Detect device orientation when Activity orientation is locked

后端 未结 3 1693
悲&欢浪女
悲&欢浪女 2020-12-06 22:27

In my project, I have to lock the orientation of an activity. (I cannot recreate the activity)

I want to display a message when the user change the orientation of th

3条回答
  •  粉色の甜心
    2020-12-06 23:04

    Below code is working for me.

    MainActivity.java:

    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            startService( new Intent(this, Receiver.class) );
    }
    }
    

    BackgroundService.java

    public class Receiver extends Service {
        private static final String BCAST_CONFIGCHANGED = "android.intent.action.CONFIGURATION_CHANGED";
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(BCAST_CONFIGCHANGED);
            this.registerReceiver(mBroadcastReceiver, filter);
        }
    
        @Override
        public void onDestroy() {
        }
    
        @Override
        public void onStart(Intent intent, int startid) {
        }
    
        public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent myIntent) {
                if ( myIntent.getAction().equals( BCAST_CONFIGCHANGED ) ) {
                    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                    }
                    else {
                    }
                }
            }
        };
    }
    

    Manifestfile.xml

    
                
                    
                
            
    

提交回复
热议问题