實(shí)現(xiàn)思路是獲取其他應(yīng)用上層懸浮窗的權(quán)限,然后在狀態(tài)欄的位置寫一個(gè)狀態(tài)欄高度的透明view
代碼如下
CustomViewGroup view;
WindowManager manager;
/**
* activity啟動(dòng)時(shí)調(diào)用禁止下拉
*/
private void prohibitDropDown() {
manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (30 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
view = new CustomViewGroup(this);
manager.addView(view, localLayoutParams);
}
/**
* activity銷毀時(shí)允許下拉,不然程序不大退朽合,怎么都無(wú)法下拉狀態(tài)欄
*/
private void allowDropDown() {
manager.removeView(view);
}
6.0以下的直接在AndroidManifest.xml里加
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
6.0以后在AndroidManifest.xml里加了權(quán)限以后還要要自己跳轉(zhuǎn)設(shè)置
Intent intent1 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent1, 11);