android锁屏软件屏蔽状态栏下拉-应用层实现

  这阵子突然想到做锁屏软件,锁屏软件具体界面的实现不说,在屏蔽通知栏下拉的时候就出现问题了。

  网上找了一些资料,可以通过statusbarmanager这个类来实现,由于这个类是系统隐藏的,所以我们很容易就想到使用反射,这个类的源码如下:

  1 /*
  2  * Copyright (C) 2007 The Android Open Source Project
  3  *
  4  * Licensed under the Apache License, Version 2.0 (the "License");
  5  * you may not use this file except in compliance with the License.
  6  * You may obtain a copy of the License at
  7  *
  8  *      http://www.apache.org/licenses/LICENSE-2.0
  9  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 
 17  
 18 package android.app;
 19 
 20 import android.content.Context;
 21 import android.os.Binder;
 22 import android.os.RemoteException;
 23 import android.os.IBinder;
 24 import android.os.ServiceManager;
 25 
 26 /**
 27  * Allows an app to control the status bar.
 28  *
 29  * @hide
 30  */
 31 public class StatusBarManager {
 32     /**
 33      * Flag for {@link #disable} to make the status bar not expandable.  Unless you also
 34      * set {@link #DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
 35      */
 36     public static final int DISABLE_EXPAND = 0x00000001;
 37 
 38     /**
 39      * Flag for {@link #disable} to hide notification icons and scrolling ticker text.
 40      */
 41     public static final int DISABLE_NOTIFICATION_ICONS = 0x00000002;
 42 
 43     /**
 44      * Flag for {@link #disable} to disable incoming notification alerts.  This will not block
 45      * icons, but it will block sound, vibrating and other visual or aural notifications.
 46      */
 47     public static final int DISABLE_NOTIFICATION_ALERTS = 0x00000004;
 48 
 49     /**
 50      * Flag for {@link #disable} to hide only the scrolling ticker.  Note that
 51      * {@link #DISABLE_NOTIFICATION_ICONS} implies {@link #DISABLE_NOTIFICATION_TICKER}.
 52      */
 53     public static final int DISABLE_NOTIFICATION_TICKER = 0x00000008;
 54 
 55     /**
 56      * Re-enable all of the status bar features that you've disabled.
 57      */
 58     public static final int DISABLE_NONE = 0x00000000;
 59 
 60     private Context mContext;
 61     private IStatusBar mService;
 62     private IBinder mToken = new Binder();
 63 
 64     StatusBarManager(Context context) {
 65         mContext = context;
 66         mService = IStatusBar.Stub.asInterface(
 67                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
 68     }
 69 
 70     /**
 71      * Disable some features in the status bar.  Pass the bitwise-or of the DISABLE_* flags.
 72      * To re-enable everything, pass {@link #DISABLE_NONE}.
 73      */
 74     public void disable(int what) {
 75         try {
 76             mService.disable(what, mToken, mContext.getPackageName());
 77         } catch (RemoteException ex) {
 78             // system process is dead anyway.
 79             throw new RuntimeException(ex);
 80         }
 81     }
 82     
 83     /**
 84      * Expand the status bar.
 85      */
 86     public void expand() {
 87         try {
 88             mService.activate();
 89         } catch (RemoteException ex) {
 90             // system process is dead anyway.
 91             throw new RuntimeException(ex);
 92         }
 93     }
 94     
 95     /**
 96      * Collapse the status bar.
 97      */
 98     public void collapse() {
 99         try {
100             mService.deactivate();
101         } catch (RemoteException ex) {
102             // system process is dead anyway.
103             throw new RuntimeException(ex);
104         }
105     }
106     
107     /**
108      * Toggle the status bar.
109      */
110     public void toggle() {
111         try {
112             mService.toggle();
113         } catch (RemoteException ex) {
114             // system process is dead anyway.
115             throw new RuntimeException(ex);
116         }
117     }
118 
119     public IBinder addIcon(String slot, int iconId, int iconLevel) {
120         try {
121             return mService.addIcon(slot, mContext.getPackageName(), iconId, iconLevel);
122         } catch (RemoteException ex) {
123             // system process is dead anyway.
124             throw new RuntimeException(ex);
125         }
126     }
127 
128     public void updateIcon(IBinder key, String slot, int iconId, int iconLevel) {
129         try {
130             mService.updateIcon(key, slot, mContext.getPackageName(), iconId, iconLevel);
131         } catch (RemoteException ex) {
132             // system process is dead anyway.
133             throw new RuntimeException(ex);
134         }
135     }
136 
137     public void removeIcon(IBinder key) {
138         try {
139             mService.removeIcon(key);
140         } catch (RemoteException ex) {
141             // system process is dead anyway.
142             throw new RuntimeException(ex);
143         }
144     }
145 }

如果是系统级应用,也就是手机厂家植入的应用,可以使用disable(int)的方法来进行屏蔽,参数如上源码五个参数之一即可。但是如果是在应用层上的,disable方法因为权限问题无法使用(如果一定要使用必须具有系统签名,签名的方法和局限在上一篇博客有介绍,不再说明)。这个时候可以使用collapse()方法,现在的小米锁屏和360锁屏都是使用该方法,具体代码如下:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        disableStatusBar();
        super.onWindowFocusChanged(hasFocus);
    }

    public void disableStatusBar(){
        try {
            Object service = getSystemService("statusbar");
            Class<?> claz = Class.forName("android.app.StatusBarManager");
            Method expand = claz.getMethod("collapse");
            expand.invoke(service);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

重写activity的onWindowfocuschanged方法,执行如上操作即可。以上方法使用了反射,如果不想使用使用反射获得隐藏的StatusBarManager,我这里提供一个jar包,将jar包导入到项目中,即可直接使用StatusBarManager ,还可以直接使用ServiceManager这个隐藏类,它有什么用?相信做过自动挂断电话的童鞋应该了解。

jar下载地址包:http://download.csdn.net/detail/welen123456789/5068165

转载于:https://www.cnblogs.com/welenwho/archive/2013/02/17/2914496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值