Android4.0 状态栏添加屏幕锁定按钮

1>frameworks\base\packages\SystemUI\res\layout-sw600dp\status_bar.xml:

<com.android.systemui.statusbar.policy.KeyButtonView 
android:id="@+id/sysbar_screenrotate"
android:layout_width="80dip" 
android:layout_height="match_parent"
android:src="@drawable/ic_sysbar_rotate" 
systemui:glowBackground="@drawable/ic_sysbar_highlight" />
android:visibility="visible"


2>frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\tablet\TabletStatusBar.java:

import android.content.ServiceConnection; 
import android.content.ComponentName;
import android.os.Messenger;
import android.util.Log;
import android.os.ServiceManager;
import android.view.Surface;
import android.view.IWindowManager;//cao
import android.provider.Settings;
import android.database.ContentObserver;
import android.content.ContentResolver;


public static boolean AUTO_ROTATE = true; //cao
public static final String TAG = "TabletStatusBar";


View mRecentButton;
View mScreenRotate;//cao


private ContentObserver mScreenRotationObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            onScreenRotateChanged();
        }
    };
 public void onScreenRotateChanged() {
                 final Context context = mContext;
                ContentResolver resolver = mContext.getContentResolver();
                Slog.d(TAG, "onScreenRotateChanged-------------");
                if (Settings.System.getInt(resolver,Settings.System.ACCELEROMETER_ROTATION, 0) != 0) {
                        AUTO_ROTATE=true;
                        ((ImageView)mScreenRotate).setImageResource(R.drawable.ic_sysbar_rotate);
                } else {
                    ((ImageView)mScreenRotate).setImageResource(R.drawable.ic_sysbar_rotate_lock);
                     AUTO_ROTATE=false;
                }            
    }


 protected View makeStatusBarView() {

//cao 20121228
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(
                Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true,
                mScreenRotationObserver);


mScreenRotate = mNavigationArea.findViewById(R.id.sysbar_screenrotate);
//set rotate icon
if (Settings.System.getInt(resolver,Settings.System.ACCELEROMETER_ROTATION, 0) == 0)
((ImageView)mScreenRotate).setImageResource(R.drawable.ic_sysbar_rotate_lock);
mScreenRotate.setOnClickListener(mOnClickListener);


 private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {

else if (v == mScreenRotate) {              
                //mScreenRotate.setBackgroundResource(R.drawable.ic_sysbar_highlight); 
                onClickScreenRotateButton();
//cao start
            }

public void onClickScreenRotateButton() {
Slog.d(TAG, "onClickScreenRotateButton-------------");
try {
      IWindowManager wm = IWindowManager.Stub.asInterface(
                        ServiceManager.getService(Context.WINDOW_SERVICE));           
              if (!AUTO_ROTATE) {
((ImageView)mScreenRotate).setImageResource(R.drawable.ic_sysbar_rotate);
                    wm.thawRotation();
                } else {
                    ((ImageView)mScreenRotate).setImageResource(R.drawable.ic_sysbar_rotate_lock);
                   //wm.freezeRotation(Surface.ROTATION_0);//锁定为0方向
                   wm.freezeRotation(wm.getRotation());//锁定为当前方向
                }
            } catch (RemoteException exc) {
                Log.w(TAG, "Unable to save auto-rotate setting");}
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值