Android 设置屏幕方向接口

本文详细介绍了如何通过自定义Service在Android系统中实现屏幕方向的控制功能。主要步骤包括:在ICustomizedService.aidl中定义接口,通过CustomizedManager.java实现接口,并在CustomizedService.java中具体实现接口功能,最终实现对屏幕方向的冻结和解冻。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义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);
  }
 
 
}
 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值