这个东搞了几天了,累啊!今天终于有个结果,同志们要找办法的只看3就行了,其他的我只用作记录过程
1.想通过,获取当前ACtivity获取,只有ACtivity才能有getWindow() 可是拿不到Activity的实例啊,
PhoneStatusBarView.java:
/* Rect outRect = new Rect();
getWindowVisibleDisplayFrame(outRect);
int statusHeight = outRect.top;
WindowManager windowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display=windowManager.getDefaultDisplay();
Log.v(TAG, "statusBarHeight =====================statusHeight= " + statusHeight);
Log.v(TAG, "statusBarHeight =====================display.getHeight()= " + display.getHeight());
//View.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
//int statusBarHeight = frame.top;
Log.v(TAG, "statusBarHeight ====================== " + statusBarHeight);*/
2.通过phoneStatusBar 的方法setSystemUiVisibility(int vis)找思路:又找到相关资料:http://blog.csdn.net/casun_li/article/details/8963622
但还是无门啊:activity.getWindow().getDecorView().getSystemUiVisibility();只能拿到自己View的Visibility值
OnSystemUiVisibilityChangeListener监听的应该是navigate bar(有待验证)
/* Activity activity=((SystemUIApplication)context.getApplicationContext()).mActivity;
Log.v(TAG, "PhoneStatusBarView +++++++++++++++++++++=activity =" + activity);
mContext=context;
setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
Log.v(TAG, "@@@@@@@@@@@@@@@@@@@onSystemUiVisibilityChange ======visibility =" +visibility);
}
});*/
/* Activity activity=((SystemUIApplication)mContext.getApplicationContext()).mActivity;
Log.v(TAG, "afterOnLayout ***********************************activity =" + activity);
if(android.os.Build.VERSION.SDK_INT>=11){
int vi=activity.getWindow().getDecorView().getSystemUiVisibility();
int thisvi=getSystemUiVisibility();
Log.v(TAG, "afterOnLayout ***********************************vi =" + vi+ " thisvi =" + thisvi);
setSystemUiVisibility(SYSTEM_UI_FLAG_HIDE_NAVIGATION);
setSystemUiVisibility(SYSTEM_UI_FLAG_LOW_PROFILE);
vi=activity.getWindow().getDecorView().getSystemUiVisibility();
thisvi=getSystemUiVisibility();
Log.v(TAG, "++++++afterOnLayout*******************************vi =" + vi+ " thisvi =" + thisvi);
activity.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
Log.v(TAG, "2@@@@@@@@@@@@@@@@@@@onSystemUiVisibilityChange ======visibility =" +visibility);
}
});
}*/
3.这才是正道啊:将一个view用WindowManager加到窗口:add ,这个View 注册上:OnGlobalLayoutListener 大功告成
写的不详细啊,请大家不要怪,网上找不到这方法啊,至少我没找到
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;//2006;
localLayoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
localLayoutParams.flags = 8;
localLayoutParams.width = 1;
localLayoutParams.height = LayoutParams.MATCH_PARENT;
localLayoutParams.format = PixelFormat.TRANSPARENT;
mWindowHelper=new View(context);
mWindowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
mScreenHeight=mWindowManager.getDefaultDisplay().getHeight();
mScreenWidth=mWindowManager.getDefaultDisplay().getWidth();
mWindowManager.addView(this.mWindowHelper, localLayoutParams);
this.mWindowHelper.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
try {
int height=PhoneStatusBarView.this.mWindowHelper.getHeight();
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======height =" +height);
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======mCurrentOrientation =" +mCurrentOrientation);
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======mScreenWidth =" +mScreenWidth);
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======mScreenHeight =" +mScreenHeight);
switch (PhoneStatusBarView.this.mCurrentOrientation) {
case Configuration.ORIENTATION_LANDSCAPE:
if(height==mScreenWidth){
//hide
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======hide...........");
mHandler.removeMessages(MSG_HIDE_STATUS_BAR);
mHandler.sendEmptyMessage(MSG_HIDE_STATUS_BAR);
//mHandler.sendEmptyMessageDelayed(MSG_HIDE_STATUS_BAR, 0);
}else if(height
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======show...........");
mHandler.removeMessages(MSG_SHOW_STATUS_BAR);
mHandler.sendEmptyMessage(MSG_SHOW_STATUS_BAR);
//mHandler.sendEmptyMessageDelayed(MSG_SHOW_STATUS_BAR, 200);
}
break;
default:
if(height==mScreenHeight){
//hide
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======hide...........");
mHandler.removeMessages(MSG_HIDE_STATUS_BAR);
mHandler.sendEmptyMessage(MSG_HIDE_STATUS_BAR);
//mHandler.sendEmptyMessageDelayed(MSG_HIDE_STATUS_BAR, 200);
}else if(height
Log.d(TAG, "@@@@@@@@@@@@@@@@@@@==onGlobalLayout() ======show...........");
mHandler.removeMessages(MSG_SHOW_STATUS_BAR);
mHandler.sendEmptyMessage(MSG_SHOW_STATUS_BAR);
//mHandler.sendEmptyMessageDelayed(MSG_SHOW_STATUS_BAR, 200);
}
break;
}
} catch (Exception e) {
// TODO: handle exception
}
}
});
引发的android.os.DeadObjectException 异常: 1.请注意注册与取消 2.将mWindowHelper放在服务中,不要放在另一个View中.(否则meizu手机出错,但其他手机正常)
private boolean mAddWindowHelper=false;
public void addWindowHelper(){
if(mAddWindowHelper)return;
Log.d(TAG, " addWindowHelper()... mOnGlobalLayoutListener= "+mOnGlobalLayoutListener);
try {
mWindowManager.addView(this.mWindowHelper, localLayoutParams);
this.mWindowHelper.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
mAddWindowHelper=true;
} catch (Exception e) {
Log.e(TAG, " Exception e: "+e.getMessage());
}
}
public void removeWindowHelper() {
if(!mAddWindowHelper)return;
Log.d(TAG, " removeWindowHelper()... mOnGlobalLayoutListener= "+mOnGlobalLayoutListener);
try {
this.mWindowHelper.getViewTreeObserver().removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
mWindowManager.removeView(this.mWindowHelper);
mAddWindowHelper=false;
} catch (Exception e) {
Log.e(TAG, " Exception e: "+e.getMessage());
}
}
4.采用3. 以前的方法:在OnLayout()方法里面调用afterLayout(): 这样就有了全屏的问题,弹出tick后,特别是在4..2.2中
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.v(TAG, "onLayout ====================== ");
super.onLayout(changed, l, t, r, b);
//afterOnLayout();
}
private void afterOnLayout(){
getWindowVisibleDisplayFrame(mRect);
int newTop = mRect.top;
Log.d(TAG,"*****************************************rc.top === "+newTop);
Log.d(TAG, "*****************************************mOldTop === "+mOldTop);
Log.d(TAG, "******************* PhoneStatusBar.mIsExpanding="+PhoneStatusBar.mIsExpanding);
Log.d(TAG, "**************************************** isRemoteEvent="+isRemoteEvent);
if (PhoneStatusBar.mIsExpanding || isRemoteEvent) {
newTop = statusBarHeight; // 拉下来时显示自己的状态栏View 当为全屏时,收回时隐藏自己的状态栏View
isRemoteEvent = false;
//服务没开 当为全屏时,有时会隐藏不了,所以这里处理一下,收回时隐藏自己的状态栏View
//因为PhoneStatusBar.mIsExpanding还是==true;
//Log.d(TAG, "+++++++++++++++++++ Service.isRuning()="+EspierNotificationsService.isRuning());
//Log.d(TAG, "+++++++++++++++++++ mService.mAnimating="+mService.mAnimating);
if(!EspierNotificationsService.isRuning() && PhoneStatusBar.mIsExpanding && mService.mAnimating){
mHandler.removeMessages(MSG_AFTER_LAYOUT);
mHandler.sendEmptyMessageDelayed(MSG_AFTER_LAYOUT, 200);
}
}
if (newTop != mOldTop) {
if (newTop == 0) {
windowManagerParams.height = 0;
RemoteStatusBarService.getInstance(getContext()).sendRemoteBroadcast();
} else {
windowManagerParams.height = statusBarHeight;
}
mOldTop = newTop;
updateViewPosition();
}
}
private void updateViewPosition() {
try {
Log.d(TAG, "&&&&&&&&&&&&&&&&&&&&&&&+ updateViewPosition().....windowManagerParams.height="+windowManagerParams.height);
//4.2.2系统: 在视频播放时状态栏就是不消失,其他更新状态栏的方法(如更新时间)全没作用:原因不明
//因此在windowManagerParams.height让状态栏Gone
if(android.os.Build.VERSION.SDK_INT>=17 ){
if(windowManagerParams.height==0){
windowManager.removeView(this);
windowManager.addView(this, windowManagerParams);
}else{
windowManager.updateViewLayout(this, windowManagerParams);
}
}else{
windowManager.updateViewLayout(this, windowManagerParams);
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "&&&&&&&&&&&&&&&&&&&&&&& updateViewPosition().....e="+e.getMessage());
}
}