当程序员去创建一个新的window的时候,发现new的时候右好多方法未实现,在android中有这样一个类可以生成一个新的window,但是由于该类被android设置为@hide类型的类,所以程序员无法使用。但是程序员总是有办法的,通过java的反射机制就可以访问到该类,并且调用到生成window的方法。贴上代码如下:
public void createWindow(){
try {
// 创建新的window
Class PolicyManagerClass = Class.forName("com.android.internal.policy.PolicyManager");
Method method = PolicyManagerClass.getMethod("makeNewWindow", Context.class);
mWindow = (Window)method.invoke(null, mContext);
// 设置window的callback,在这里将处理touch,keyevent等等
mWindow.setCallback(this);
mWindow.setContentView(mShortCut);
// 将window与windowmanager绑定在一起
mWindow.setWindowManager(mWindowManager, null, null);
mParams.gravity = Gravity.LEFT | Gravity.TOP;
mParams.type = LayoutParams.TYPE_PHONE;
mParams.width = LayoutParams.MATCH_PARENT;
mParams.height = LayoutParams.MATCH_PARENT;
// 让view在界面中显示
mWindowManager.addView(mWindow.getDecorView(), mParams);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}