smartwindow

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

适用android2.x 在应用程序中自定义dialog,复制dialog.java到应用程序中并修改方法
 public void setContentView(int layoutResID) {
        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        mWindow.setContentView(layoutResID);
    }
    public void setContentView(View view) {
    	mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        mWindow.setContentView(view);
    }
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    	mWindow.requestFeature(Window.FEATURE_NO_TITLE);
    	mWindow.setContentView(view, params);
    }
    public void show(boolean focusable) {
        if (mShowing) {
            if (mDecor != null) {
                mDecor.setVisibility(View.VISIBLE);
            }
            return;
        }
        if (!mCreated) {
            dispatchOnCreate(null);
        }
        onStart();
        mDecor = mWindow.getDecorView();
        WindowManager.LayoutParams l = mWindow.getAttributes();
        if ((l.softInputMode
                & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
            WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
            nl.copyFrom(l);
            nl.softInputMode |=
                    WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
            l = nl;
        }
        l.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 
        		| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        l.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
        		| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        if (!focusable) {
        	l.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 
        	| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        }
        try {
            mWindowManager.addView(mDecor, l);
            mShowing = true;
            sendShowMessage();
        } finally {
        }
    }
    private static final String POLICY_IMPL_CLASS_NAME =
        "com.android.internal.policy.impl.Policy";
    private static final Object sPolicy;
    private static final Class policyClass;
    static {
        // Pull in the actual implementation of the policy at run-time
        try {
            policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
            sPolicy = policyClass.newInstance();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(
                    POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(
                    POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
        }
    }
    public SmartWindow(Context context, int theme) {
    	//由于SmartWindow的生命周期有可能会超出调用它的对象(Activity,Service)
    	//比如Activity已经finish掉了,但是SmartWindow没有dismiss掉,
    	//则会导致内存泄漏,因此这里强制使用ApplicationContext
        mContext = new ContextThemeWrapper(
        		context.getApplicationContext(), theme == 0 ? android.R.style.Theme_InputMethod : theme);
        mWindowManager = (WindowManager)context.getApplicationContext().getSystemService("window");
		Window w = null;
		try {
			Method m = policyClass.getMethod("makeNewWindow", Context.class);
			w = (Window)m.invoke(sPolicy, mContext);
		} catch (IllegalArgumentException e) {
			Log.e(TAG, e.getMessage()+"");
		} catch (IllegalAccessException e) {
			Log.e(TAG, e.getMessage()+"");
		} catch (InvocationTargetException e) {
			Log.e(TAG, e.getMessage()+"");
		} catch (SecurityException e) {
			Log.e(TAG, e.getMessage()+"");
		} catch (NoSuchMethodException e) {
			Log.e(TAG, e.getMessage()+"");
		}
        mWindow = w;
        w.setCallback(this);
        w.setWindowManager(mWindowManager, null, null);
        w.setGravity(Gravity.CENTER);
        mUiThread = Thread.currentThread();
        mListenersHandler = new ListenersHandler(this);
    }
 

你可能感兴趣的:(window)