smartwindow
16lz
2021-01-23
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
适用android2.x 在应用程序中自定义dialog,复制dialog.java到应用程序中并修改方法
<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); }
更多相关文章
- Android 应用程序基础(Application Fundamentals)----Application
- 应用程序组件之APP基础(转官方)
- Android Training - 建立你的第一个应用程序
- Android开发指南 ──应用程序基础
- android 跨应用程序广播发送接受
- Android应用程序线程消息循环模型分析(1)