1

I am creating an application which will change the orientation of some other already installed application, Reading the orientation of other application installed was simple through ApplicationInfo object but no way to change it.

After some R&D I have come to notice similar types of apps (which change orientation) also run Service, that means they make orientation change on fly but I don't know how exactly they do it.

For example like Smart Rotator app does it https://play.google.com/store/apps/details?id=net.xdevelop.rotator_t&hl=en

Mr Coder
  • 8,169
  • 5
  • 45
  • 74

2 Answers2

2

The other apps that you reference do something different to what you might be expecting. They enable screen rotation based on the device's accelerometer; that is, the user rotates the device and the orientation changes. They do not set a specific orientation for any application. Note that if you disable automatic rotation whilst in landscape mode, the orientation typically returns to portrait.

I have done the same as these other applications before. Unfortunately it's proprietary commercial code that I'm not in a position to share.

There are two components to the approach:

  1. Detect when the foreground application is the target app
  2. Change the system-wide preference for display orientation

Obviously you have to do the reverse too, i.e. change it back afterwards.

There are some examples of piece 1 elsewhere on SO. In my case I wrote a service that continuously polled to see what the foreground app is (because there are no relevant system events to inform of app change). It's not a trivial undertaking but it can be done.

The relevant setting for piece 2 is Settings.System.ACCELEROMETER_ROTATION. I think you can write that with an ordinary app permission on all versions. The code snippet for that is below:

        Settings.System.putInt(
             app.getContentResolver(),
             Settings.System.ACCELEROMETER_ROTATION,
             orientationEnabled ? 1 : 0
        );

Note also that it is possible to write an application that will ignore the system's screen rotation; i.e. will stay locked in whatever orientation it was designed for. It's not common though.

Rob Pridham
  • 4,780
  • 1
  • 26
  • 38
  • thanks for your answer , can you provide pseudo code for piece 1 aswell i.e polling to check app running and end status ? – Mr Coder Feb 01 '13 at 08:38
  • Not as such, but [this](http://stackoverflow.com/questions/6363207/android-background-service-to-determine-foreground-application) looks like a reasonable starting point. In practice you will find there are various subtleties; as one example, applications that are invoked to handle a broadcast but do not visually appear are treated as being in the foreground - but that's the sort of thing you should probably address with a new SO question when you encounter it. – Rob Pridham Feb 01 '13 at 09:27
1

You can do this by interprocess communication. You have several options:

1- You can use SharedPreferences. Set a WORLD_READABLE preference from your application to be read on every startup from your target application. Have a look at this.

2- You can write this preference in a file on sdcard and read it in the same way.

3- If your target application is running (that is, it has a Service or something else so its process is alive) you can broadcast an intent and receive it from your target application. Here you can find a post about this subject.

4- ... In short you need to pass a parameter to your target application and set your orientation as in setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Community
  • 1
  • 1
Alpay
  • 1,350
  • 2
  • 24
  • 56
  • The app whose orientation I would like to change is not my application – Mr Coder Jan 31 '13 at 02:32
  • The applications on Android devices are all reside in their seperate sandboxes. Unless both the changing and the changer application is yours and you design them in a special way I mentioned above, It _will_ and _should_ be impossible to change another application' s properties, like screen orientation. Would you want some irrelevant application to change your application' s properties without your permission? – Alpay Jan 31 '13 at 07:33
  • it might look impossible but here is an app which does it https://play.google.com/store/apps/details?id=net.xdevelop.rotator_t&hl=en – Mr Coder Jan 31 '13 at 09:45