Android 设置屏幕方向接口

自定义service完成,我只把主要修改的几个文件列出

1,在frameworks/base/core/java/android/app/customized/ICustomizedService.aidl中定义接口

package android.app.customized;
 
interface ICustomizedService{
    void setRotationLock(boolean enabled,int rotation);
}

2,在frameworks/base/core/java/android/app/customized/CustomizedManager.java中实现接口

package android.app.customized;
 
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.RemoteException;
import android.provider.Settings;
import java.io.IOException;
import android.os.ServiceManager;
import android.os.IBinder;
import java.util.List;
import android.app.ActivityManager;
import android.graphics.Bitmap;
 
 
public class CustomizedManager{
    private static final String TAG="CustomizedManager";
    private static final boolean DBG=true;
    
    private static ICustomizedService mService;
    private final Context mContext;
 
 
    public CustomizedManager(Context context){
        mContext = context;
        mService = ICustomizedService.Stub.asInterface(
                ServiceManager.getService("customized"));
    }
    private static ICustomizedService getService(){
        if (mService != null) {
            return mService;
        }
        
        IBinder b = ServiceManager.getService("customized"
        mService = ICustomizedService.Stub.asInterface(b);
        return mService;
    }
 
    public void setRotationLock(boolean enabled,int rotation);{
	    ICustomizedService service = getService();
        try {
            service.setRotationLock(enabled,rotation);
        } catch (RemoteException e) {}
    }
 
}

3,在frameworks/base/services/core/java/com/android/server/customized/CustomizedService.java中具体实现接口

package com.android.server.customized;
 
import android.os.IBinder;
import android.os.ServiceManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.app.customized.ICustomizedService;
import android.content.BroadcastReceiver;
import android.view.IWindowManager;
import android.view.WindowManagerGlobal;

 
public class CustomizedService extends ICustomizedService.Stub {
    private static final String TAG = "CustomizedService ";
    private Context mContext;
 
    public static class Lifecycle extends SystemService {
        private CustomizedService mService;
 
       public Lifecycle(Context context) {
            super(context);
        }
 
        @Override
        public void onStart() {
            mService = new CustomizedService (getContext());
            publishBinderService(Context.CUSTOMIZED, mService);
        }
 
       @Override
        public void onBootPhase(int phase) {
        }
 
        @Override
        public void onUnlockUser(int userHandle) {
        }
    }
 
    public CustomizedService (Context context) {
       mContext = context;
   }
 
    
    public void setRotationLock(boolean enabled,int rotation) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                    if (enabled) {    //enabled为true时表示冻结自动旋转,rotation为0是竖屏,为1是横屏
                        wm.freezeRotation(rotation);
                    } else {
                        wm.thawRotation();
                    }
                } catch (RemoteException exc) {
                    Log.w(TAG, "Unable to save auto-rotate setting");
                }
            }
        });

    }
 
    private void sentControl(String action, String key, boolean iscontrol) {
       long jh = Binder.clearCallingIdentity();
       Intent i = new Intent();
       i.setAction(action);
       if (iscontrol) {
           i.putExtra(key, true);
       } else {
           i.putExtra(key, false);
       }
       mContext.sendBroadcast(i);
       sentControl (action,iscontrol);
       Binder.restoreCallingIdentity(jh);
    }
 
    private void sentControl(String action, boolean iscontrol) {
       long jh = Binder.clearCallingIdentity();
       int key;
       if (iscontrol) {
           key = 0;
       } else {
           key = 1;
      }
       Log.i ("custom",",action "+ action + ";   key"+key);   
       Settings.Secure.putInt(mContext.getContentResolver(),action,key);
      Binder.restoreCallingIdentity(jh);
  }
 
 
}
 

如此设置之后,设置屏幕方向的接口就实现了~

猜你喜欢

转载自blog.csdn.net/lancelots/article/details/82787264