主要步骤 |
---|
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);
}
}
});
}