Android 4.2 禁止下拉状态栏

来源:互联网 发布:js实例子 编辑:程序博客网 时间:2024/05/10 22:13

最近在做一个界面需要禁止下拉状态栏,于是整理了一下,以后备用。

import android.app.StatusBarManager;//首先导入包


StatusBarManager mStatusBar = (StatusBarManager) getSystemService(this.STATUS_BAR_SERVICE);//获得对象
mStatusBar.disable(StatusBarManager.DISABLE_EXPAND);//禁止下拉状态栏


mStatusBar.disable(StatusBarManager.DISABLE_NONE);//启用下拉状态栏(在onDestroy()添加)


//此代码只能在源码环境下才能编译


---------------------------------------------------源码中是这样定义的--------------------------------------------------------

source/frameworks/base/core/java/android/app/StatusBarManager.java

public static final int DISABLE_EXPAND = View.STATUS_BAR_DISABLE_EXPAND;

public static final int DISABLE_NONE = 0x00000000;


/**
     * Disable some features in the status bar.  Pass the bitwise-or of the DISABLE_* flags.
     * To re-enable everything, pass {@link #DISABLE_NONE}.
     */
    public void disable(int what) {
        try {
            final IStatusBarService svc = getService();//获得IStatusBarService 的对象
            if (svc != null) {
                svc.disable(what, mToken, mContext.getPackageName());
//实际上使用的是IStatusBarService的disable方法
            }
        } catch (RemoteException ex) {
            // system process is dead anyway.
            throw new RuntimeException(ex);
        }
    }


private synchronized IStatusBarService getService() {
        if (mService == null) {
            mService = IStatusBarService.Stub.asInterface(
                    ServiceManager.getService(Context.STATUS_BAR_SERVICE));
//使AIDL获取IStatusBarService对象
            if (mService == null) {
                Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
            }
        }
        return mService;
    }


source/frameworks/base/services/java/com/android/server/StatusBarManagerService.java

    public void disable(int what, IBinder token, String pkg) {
        disableInternal(mCurrentUserId, what, token, pkg);
    }


    private void disableInternal(int userId, int what, IBinder token, String pkg) {
        enforceStatusBar();

        synchronized (mLock) {
            disableLocked(userId, what, token, pkg);
        }
    }


   private void disableLocked(int userId, int what, IBinder token, String pkg) {
        // It's important that the the callback and the call to mBar get done
        // in the same order when multiple threads are calling this function
        // so they are paired correctly.  The messages on the handler will be
        // handled in the order they were enqueued, but will be outside the lock.
        manageDisableListLocked(userId, what, token, pkg);
        final int net = gatherDisableActionsLocked(userId);
        if (net != mDisabled) {
            mDisabled = net;
            mHandler.post(new Runnable() {
                    public void run() {
                        mNotificationCallbacks.onSetDisabled(net);
                    }
                });
            if (mBar != null) {
                try {
                    mBar.disable(net);
                } catch (RemoteException ex) {
                }
            }
        }
    }

source/frameworks/base/core/java/android/view/View.java

    /**
     * @hide
     *
     * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
     * out of the public fields to keep the undefined bits out of the developer's way.
     *
     * Flag to make the status bar not expandable.  Unless you also
     * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
     */
    public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;





0 0