android 刷新隐藏提示,Android-WindowManager 显示/隐藏/状态更新

主要步骤

1.RemoteViewManager类对自定义WindowManager显示/隐藏/状态更新进行管理

2.自定义WindowManager类

3.提供出显示/隐藏/状态刷新接口

注意点

1.自定义WindowManager初始化布局位置属性设置

2.频繁操作隐藏和显示,出现隐藏不了问题

1.抽象出WindowManager状态的接口

public interface IRemoteCtrl {

/**

* 显示

*/

public void showWindow();

/**

* 隐藏

*/

public void hideWindow();

/**

* 状态监听

*/

public void enterPage(String page);

}

2.自定义WindowManager类

加载xml文件-初始化其相关的属性值-状态更新

public class BTRemoteWindow extends RelativeLayout{

private static final String TAG BTRemoteWindow.class.getSimpleName();

Context context;

private WindowManager.LayoutParams btWindowParams;

private View gView;

private WindowManager mWindowManager;

private TextView type;

private TextView talkTime;

private LinearLayout mSwitchLayout;

public BTRemoteWindow(Context context) {

super(context);

this.context = context;

mWindowManager = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

gView = LayoutInflater.from(context).inflate(R.layout.view_remote_window, null);

initView(gView);

addView(gView);

}

private void initView(View gView) {

type = (TextView)gView.findViewById(R.id.type);

talkTime = (TextView)gView.findViewById(R.id.talkTime);

mSwitchLayout = (LinearLayout) gView.findViewById(R.id.switchToTalkActivity);

mSwitchLayout.setOnClickListener(mOnClickListener);

}

/**

* 单击事件

*/

private OnClickListener mOnClickListener = new OnClickListener() {

@Override

public void onClick(View view) {

switch(view.getId()) {

case R.id.switchToTalkActivity:

break;

}

}

};

/**

* 初始化布局位置

*/

private WindowManager.LayoutParams windowParams() {

if (btWindowParams == null) {

btWindowParams = new WindowManager.LayoutParams();

btWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;

btWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

//| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN

| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL

| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM

| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR

| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;

btWindowParams.format = PixelFormat.RGBA_8888;

btWindowParams.gravity = Gravity.LEFT;

}

//btWindowParams.width = 70;

//btWindowParams.height = 400;

//btWindowParams.x = 0;

btWindowParams.y = 0;

return btWindowParams;

}

/**

* 显示小窗口

*/

public void showWindow() {

Log.i(TAG, "showWindow");

if (this.getParent() == null) {

mWindowManager.addView(this, windowParams());

} else {

mWindowManager.updateViewLayout(this, windowParams());

}

}

/**

* 隐藏小窗口

*/

public void hideWindow() {

Log.i(TAG, "hideWindow");

if (this.getParent() != null) {

mWindowManager.removeView(this);

}

}

/**

* 设置状态值

*/

public void setTalkType(String typeStr){

type.setText(typeStr);

}

}

3. RemoteViewManager类对自定义WindowManager显示/隐藏/状态更新进行管理

public class RemoteViewManager implements IRemoteCtrl, ThreadForever{

private RemoteViewManager() { }

private static RemoteViewManager mRemoteViewManager;

public static RemoteViewManager getInstance() {

if (mRemoteViewManager == null) {

mRemoteViewManager = new RemoteViewManager();

}

return mRemoteViewManager;

}

private static final String TAG = RemoteViewManager.class.getSimpleName();

private Context context;

/** 远端窗口 */

private BTRemoteWindow mBTRemoteWindow;

/** 当前是否显示窗口 */

private boolean mIsShow = false;

Handler handler;

/**

* 构造弹出框

*/

public void init(Context context) {

this.context = context;

mBTRemoteWindow = new BTRemoteWindow(context);

handler = new Handler();

}

private boolean isInitOK() {

return this.context != null;

}

/**

* 显示

*/

public void showWindow() {

if (!isInitOK()) {

L.i(TAG, "尚未初始化,show请求失败!");

return;

}

if (!mIsShow) {

mIsShow = true;

handler.post(new Runnable() {

@Override

public void run() {

mBTRemoteWindow.showWindow();

}

});

}

}

/**

* 隐藏

*/

public void hideWindow() {

if (!isInitOK()) {

L.i(TAG, "尚未初始化,hide请求失败!");

return;

}

if (mIsShow) {

mIsShow = false;

handler.post(new Runnable() {

@Override

public void run() {

mBTRemoteWindow.hideWindow();

}

});

}

}

public boolean isShow() {

return mIsShow;

}

/**

* 状态监听

*/

public void enterPage(final String page) {

if (!isInitOK()) {

L.i(TAG, "尚未初始化,notifyState请求失败!");

return;

}

if (page == null || "".equals(page.trim())) {

L.w(TAG, "页面参数传入异常:" + page);

return;

}

handler.post(new Runnable() {

@Override

public void run() {

String nameStr = null; //自己赋值

String numStr = null; //自己赋值

if (T.PageCmd.PAGE_COMING.equals(page)) {

mBTRemoteWindow.enterComming(nameStr, numStr);

} else if (T.PageCmd.PAGE_OUTING.equals(page)) {

mBTRemoteWindow.enterOuting(nameStr, numStr);

} else if (T.PageCmd.PAGE_TALKING.equals(page)) {

mBTRemoteWindow.enterTalking(nameStr, numStr);

}

}

});

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值