Q:

Changing orientation of another application

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

A:

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:

Detect when the foreground application is the target app
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.

