Java Code Examples for android.view.WindowManager#addView()
The following examples show how to use
android.view.WindowManager#addView() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatToast.java From MobileGuard with MIT License | 6 votes |
/** * Show the view in front of screen at position x,y until call close() * @param x the position x of view * @param y the position y of view */ public void show(int x, int y) { mParams.x = x; mParams.y = y; if (mView != mNextView) { // remove the old view if necessary close(); mView = mNextView; Context context = mView.getContext().getApplicationContext(); if (context == null) { context = mView.getContext(); } mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); if (mView.getParent() != null) { mWM.removeView(mView); } mWM.addView(mView, mParams); } }
Example 2
Source File: FloatingButtonService.java From WhatsAppStatusSaver with Apache License 2.0 | 6 votes |
@SuppressLint("InflateParams") public WindowManager.LayoutParams setUpAllViews() { final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.START; //Initially view will be added to top-left corner params.x = 0; params.y = 100; //Add the view to the window mWindowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE); try { if (mWindowManager != null) { mWindowManager.addView(mFloatingView, params); } }catch (Exception e){ e.printStackTrace(); } return params; }
Example 3
Source File: GridOverlayService.java From android-grid-wichterle with Apache License 2.0 | 6 votes |
private void showGrid() { Log.d(Constants.TAG, "GridOverlayService.showGrid()"); GridOverlayService.sIsGridShown = true; final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT ); final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); mGridOverlay = new GridOverlay(this); wm.addView(mGridOverlay, lp); }
Example 4
Source File: FloatingDrawerView.java From FastAccess with GNU General Public License v3.0 | 5 votes |
private void setupParams(@NonNull WindowManager windowManager) { this.windowManager = windowManager; originalParams = new WindowManager.LayoutParams(TYPE_PRIORITY_PHONE, FLAG_WATCH_OUTSIDE_TOUCH | FLAG_NOT_TOUCH_MODAL, TRANSLUCENT); Point szWindow = new Point(); windowManager.getDefaultDisplay().getSize(szWindow); updateParams(ViewHelper.isLandscape(drawerHolder.appDrawer.getResources()) ? 2 : 1, false); originalParams.gravity = Gravity.CENTER; windowManager.addView(drawerHolder.appDrawer, originalParams); drawerHolder.appDrawer.animate().scaleX(1f).scaleY(1f); }
Example 5
Source File: FloatWindowManager.java From FloatWindow with Apache License 2.0 | 5 votes |
/** * 创建大悬浮窗 * * @param context * 必须为应用程序的Context. */ public void createBigWindow(Context context) { WindowManager windowManager = getWindowManager(); if (bigWindow == null) { bigWindow = new FloatWindowBigView(context); windowManager.addView(bigWindow, bigWindow.bigWindowParams); } }
Example 6
Source File: FloatService.java From Float-Bar with Eclipse Public License 1.0 | 5 votes |
private void createFloatView() { wmParams = Util.getParams(wmParams); // 悬浮窗默认显示以左上角为起始坐标 wmParams.gravity = Gravity.RIGHT| Gravity.TOP; if (!prefs.isRightMode()) { wmParams.gravity = Gravity.LEFT | Gravity.TOP; } // 以屏幕右上角为原点,设置x、y初始值,确定显示窗口的起始位置 wmParams.x = 0; wmParams.y = 0; mWindowManager = (WindowManager) service.getSystemService(Context.WINDOW_SERVICE); LayoutInflater inflater = LayoutInflater.from(service); // 获取浮动窗口视图所在布局 mFloatLayout = (LinearLayout) inflater.inflate(R.layout.floating, null); // 添加悬浮窗的视图 mWindowManager.addView(mFloatLayout, wmParams); /** * 设置悬浮窗的点击、滑动事件 */ sampleFloat = (ImageButton) mFloatLayout.findViewById(R.id.float_button_id); sampleFloat.getBackground().setAlpha(150); sampleFloat.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { prefs.doTouch(service); } }); /** * 设置有无反馈 */ MyOnGestureListener listener = new MyOnGestureListener(); @SuppressWarnings("deprecation") final GestureDetector mGestureDetector = new GestureDetector(listener); sampleFloat.setOnTouchListener(new MyOnTouchListener(mGestureDetector)); }
Example 7
Source File: FloatService.java From Float-Bar with Eclipse Public License 1.0 | 5 votes |
private void createFloatView() { wmParams = Util.getParams(wmParams); // 悬浮窗默认显示以左上角为起始坐标 wmParams.gravity = Gravity.RIGHT| Gravity.TOP; if (!prefs.isRightMode()) { wmParams.gravity = Gravity.LEFT | Gravity.TOP; } // 以屏幕右上角为原点,设置x、y初始值,确定显示窗口的起始位置 wmParams.x = 0; wmParams.y = 0; mWindowManager = (WindowManager) service.getSystemService(Context.WINDOW_SERVICE); LayoutInflater inflater = LayoutInflater.from(service); // 获取浮动窗口视图所在布局 mFloatLayout = (LinearLayout) inflater.inflate(R.layout.floating, null); // 添加悬浮窗的视图 mWindowManager.addView(mFloatLayout, wmParams); /** * 设置悬浮窗的点击、滑动事件 */ sampleFloat = (ImageButton) mFloatLayout.findViewById(R.id.float_button_id); sampleFloat.getBackground().setAlpha(150); sampleFloat.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { prefs.doTouch(service); } }); /** * 设置有无反馈 */ MyOnGestureListener listener = new MyOnGestureListener(); @SuppressWarnings("deprecation") final GestureDetector mGestureDetector = new GestureDetector(listener); sampleFloat.setOnTouchListener(new MyOnTouchListener(mGestureDetector)); }
Example 8
Source File: BaseFloatingView.java From FastAccess with GNU General Public License v3.0 | 5 votes |
@SuppressLint("InflateParams") protected BaseFloatingView(@NonNull Context context, boolean isHorizontal) { EventBus.getDefault().register(this); this.isHorizontal = isHorizontal; this.context = context; if (!PermissionsHelper.isSystemAlertGranted(context)) { context.stopService(new Intent(context, FloatingService.class)); return; } windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); windowManager.getDefaultDisplay().getSize(szWindow); originalParams = new WindowManager.LayoutParams(TYPE_PRIORITY_PHONE, FLAG_NOT_TOUCH_MODAL | FLAG_WATCH_OUTSIDE_TOUCH | FLAG_NOT_FOCUSABLE, TRANSLUCENT); setupParamsSize(); boolean isAutoSavePosition = PrefHelper.getBoolean(PrefConstant.FA_AUTO_SAVE_POSITION); originalParams.x = isAutoSavePosition ? PrefHelper.getInt(PrefConstant.POSITION_X) : 0; originalParams.y = isAutoSavePosition ? PrefHelper.getInt(PrefConstant.POSITION_Y) : 100; originalParams.gravity = GravityCompat.START | Gravity.TOP; floatingView = new FloatingView(context, getPresenter()); onUpdateXY(); windowManager.addView(floatingView, originalParams); layoutHolder = new FloatingWindowsViewHolder(LayoutInflater.from(context).inflate(isHorizontal ? R.layout.horizontal_layout : R.layout .vertical_layout, null, false), this); layoutHolder.recycler.setAdapter(getAdapter()); windowManager.addView(layoutHolder.tabBar, originalParams); getLoader(); moveToEdge(); }
Example 9
Source File: FiamWindowManager.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
/** Inflate the container into a new popup window */ public void show(@NonNull final BindingWrapper bindingWrapper, @NonNull Activity activity) { if (isFiamDisplayed()) { Logging.loge("Fiam already active. Cannot show new Fiam."); return; } InAppMessageLayoutConfig config = bindingWrapper.getConfig(); final WindowManager.LayoutParams layoutParams = getLayoutParams(config, activity); final WindowManager windowManager = getWindowManager(activity); final View rootView = bindingWrapper.getRootView(); windowManager.addView(rootView, layoutParams); // Set 'window' left and right padding from the inset, this prevents // anything from touching the navigation bar when in landscape. Rect insetDimensions = getInsetDimensions(activity); Logging.logdPair("Inset (top, bottom)", insetDimensions.top, insetDimensions.bottom); Logging.logdPair("Inset (left, right)", insetDimensions.left, insetDimensions.right); // TODO: Should use WindowInsetCompat to make sure we don't overlap with the status bar // action bar or anything else. This will become more pressing as notches // become more common on Android phones. if (bindingWrapper.canSwipeToDismiss()) { SwipeDismissTouchListener listener = getSwipeListener(config, bindingWrapper, windowManager, layoutParams); bindingWrapper.getDialogView().setOnTouchListener(listener); } this.bindingWrapper = bindingWrapper; }
Example 10
Source File: SetupActivity.java From heads-up with GNU General Public License v3.0 | 5 votes |
private void checkStep2() { boolean success; try { WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); final View view = new View(getApplicationContext()); int type = Build.VERSION.SDK_INT >= 26 ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PRIORITY_PHONE; windowManager.addView(view, new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, type, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT )); windowManager.removeView(view); success = true; } catch (Exception e) { success = false; } if (!success) { findViewById(Build.VERSION.SDK_INT >= 23 ? R.id.popup_permission_marshmallow : R.id.popup_permission_alternative) .setVisibility(View.VISIBLE); } else { findViewById(Build.VERSION.SDK_INT >= 23 ? R.id.popup_permission_marshmallow : R.id.popup_permission_alternative) .setVisibility(View.GONE); checkStep3(); } }
Example 11
Source File: FloatView.java From LLApp with Apache License 2.0 | 5 votes |
private void init(Context mContext) { this.mContext = mContext; mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); // 更新浮动窗口位置参数 靠边 DisplayMetrics dm = new DisplayMetrics(); // 获取屏幕信息 mWindowManager.getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; mScreenHeight = dm.heightPixels; Log.e("FloatView-----","宽"+mScreenWidth+"----"+"高"+mScreenHeight); this.mWmParams = new WindowManager.LayoutParams(); // 设置window type if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mWmParams.type = WindowManager.LayoutParams.TYPE_TOAST; } else { mWmParams.type = WindowManager.LayoutParams.TYPE_PHONE; } // 设置图片格式,效果为背景透明 mWmParams.format = PixelFormat.RGBA_8888; // 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作) mWmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 调整悬浮窗显示的停靠位置为左侧置�? mWmParams.gravity = Gravity.LEFT | Gravity.TOP; mScreenHeight = mWindowManager.getDefaultDisplay().getHeight(); // 以屏幕左上角为原点,设置x、y初始值,相对于gravity mWmParams.x = 0; mWmParams.y = mScreenHeight / 2; // 设置悬浮窗口长宽数据 mWmParams.width = LayoutParams.WRAP_CONTENT; mWmParams.height = LayoutParams.WRAP_CONTENT; addView(createView(mContext)); mWindowManager.addView(this, mWmParams); mTimer = new Timer(); hide(); }
Example 12
Source File: Darkness.java From NightSight with Apache License 2.0 | 5 votes |
private void createView() { Log.i(TAG, "View created"); // myView = new LinearLayout(new ContextThemeWrapper(getApplicationContext(), R.style.Theme_Transparent), null, 0); myView = new LinearLayout(getApplicationContext()); wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); wmParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_FULLSCREEN, PixelFormat.TRANSLUCENT); /** * * Window type: phone. These are non-application windows providing * user interaction with the phone (in particular incoming calls). * These windows are normally placed above all applications, but behind * the status bar. */ wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_FULLSCREEN; wmParams.gravity = Gravity.START | Gravity.TOP; wmParams.x = 0; wmParams.y = 0; myView.setBackgroundColor(ContextCompat. getColor(this, sharedPreferences.getInt(SharedPrefs.KEY_COLOR_ID, R.color.black))); wmParams.alpha = (1.0f - ((BRIGHTNESS + 55) / (float) 255)); myView.setClickable(false); wm.addView(myView, wmParams); }
Example 13
Source File: KcaMapHpPopupService.java From kcanotify_h5-master with GNU General Public License v3.0 | 4 votes |
@Override public void onCreate() { super.onCreate(); active = true; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(getApplicationContext())) { // Can not draw overlays: pass stopSelf(); } else { broadcaster = LocalBroadcastManager.getInstance(this); dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION); data_receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //String s = intent.getStringExtra(KCA_MSG_DATA); String s = dbHelper.getValue(DB_KEY_BATTLEINFO); broadcaster.sendBroadcast(new Intent(KCA_MSG_BATTLE_VIEW_REFRESH)); Log.e("KCA", "KCA_MSG_BATTLE_INFO Received: \n".concat(s)); } }; LocalBroadcastManager.getInstance(this).registerReceiver((data_receiver), new IntentFilter(KCA_MSG_BATTLE_INFO)); LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); notificationManager = NotificationManagerCompat.from(getApplicationContext()); mView = mInflater.inflate(R.layout.view_map_hp, null); mView.setOnTouchListener(mViewTouchListener); ((TextView) mView.findViewById(R.id.view_hp_title)).setText(getStringWithLocale(R.string.viewmenu_maphp_title)); mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); popupWidth = mView.getMeasuredWidth(); popupHeight = mView.getMeasuredHeight(); hp_info = (TextView) mView.findViewById(R.id.hp_info); mParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, getWindowLayoutType(), WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); mParams.gravity = Gravity.TOP | Gravity.START; Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Point size = new Point(); display.getSize(size); screenWidth = size.x; screenHeight = size.y; Log.e("KCA", "w/h: " + String.valueOf(screenWidth) + " " + String.valueOf(screenHeight)); mParams.x = (screenWidth - popupWidth) / 2; mParams.y = (screenHeight - popupHeight) / 2; mManager = (WindowManager) getSystemService(WINDOW_SERVICE); mManager.addView(mView, mParams); } }
Example 14
Source File: StarshineAnimationProcess.java From MainScreenShow with GNU General Public License v2.0 | 4 votes |
@Override public void StartAnimation(WindowManager Wm, LayoutParams wmParams) { Wm.addView(win, wmParams); }
Example 15
Source File: VoIPActivity.java From Telegram-FOSS with GNU General Public License v2.0 | 4 votes |
private void showDebugAlert() { if (VoIPService.getSharedInstance() == null) return; VoIPService.getSharedInstance().forceRating(); final LinearLayout debugOverlay = new LinearLayout(this); debugOverlay.setOrientation(LinearLayout.VERTICAL); debugOverlay.setBackgroundColor(0xCC000000); int pad = AndroidUtilities.dp(16); debugOverlay.setPadding(pad, pad * 2, pad, pad * 2); TextView title = new TextView(this); title.setTextColor(0xFFFFFFFF); title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); title.setTypeface(Typeface.DEFAULT_BOLD); title.setGravity(Gravity.CENTER); title.setText(getDebugTitle()); debugOverlay.addView(title, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 0, 0, 0, 16)); ScrollView scroll = new ScrollView(this); final TextView debugText = new TextView(this); debugText.setTypeface(Typeface.MONOSPACE); debugText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11); debugText.setMaxWidth(AndroidUtilities.dp(350)); debugText.setTextColor(0xFFFFFFFF); debugText.setText(getFormattedDebugString()); scroll.addView(debugText); debugOverlay.addView(scroll, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 1f)); TextView closeBtn = new TextView(this); closeBtn.setBackgroundColor(0xFFFFFFFF); closeBtn.setTextColor(0xFF000000); closeBtn.setPadding(pad, pad, pad, pad); closeBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); closeBtn.setText(LocaleController.getString("Close", R.string.Close)); debugOverlay.addView(closeBtn, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL, 0, 16, 0, 0)); final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(debugOverlay, new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION_PANEL, 0, PixelFormat.TRANSLUCENT)); closeBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { wm.removeView(debugOverlay); } }); final Runnable r = new Runnable() { @Override public void run() { if (isFinishing() || VoIPService.getSharedInstance() == null) { return; } title.setText(getDebugTitle()); debugText.setText(getFormattedDebugString()); debugOverlay.postDelayed(this, 500); } }; debugOverlay.postDelayed(r, 500); }
Example 16
Source File: LinphoneGL2JNIViewOverlay.java From linphone-android with GNU General Public License v3.0 | 4 votes |
@Override public void addToWindowManager(WindowManager windowManager, WindowManager.LayoutParams params) { windowManager.addView(this, params); }
Example 17
Source File: FlyBitch.java From FloatingExample with Apache License 2.0 | 4 votes |
@Override public void onCreate() { super.onCreate(); windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); chatHead = new ImageView(this); chatHead.setImageResource(R.drawable.floating); final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.LEFT; params.x = 0; params.y = 100; windowManager.addView(chatHead, params); try { chatHead.setOnTouchListener(new View.OnTouchListener() { private WindowManager.LayoutParams paramsF = params; private int initialX; private int initialY; private float initialTouchX; private float initialTouchY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Get current time in nano seconds. initialX = paramsF.x; initialY = paramsF.y; initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_MOVE: paramsF.x = initialX + (int) (event.getRawX() - initialTouchX); paramsF.y = initialY + (int) (event.getRawY() - initialTouchY); windowManager.updateViewLayout(chatHead, paramsF); break; } return false; } }); } catch (Exception e) { // TODO: handle exception } }
Example 18
Source File: DebugOverlayView.java From debugoverlay with Apache License 2.0 | 4 votes |
public DebugOverlayView(Context context) { super(context); windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Point windowDimen = new Point(); windowManager.getDefaultDisplay().getSize(windowDimen); int desiredLayoutHeight = dpToPx(context, 100); int layoutHeight = desiredLayoutHeight < windowDimen.y ? desiredLayoutHeight : windowDimen.y; // Setup the GUI // Close Button int buttonHeight = dpToPx(context, 40); closeButton = new ImageView(context); closeButton.setImageResource(R.drawable.ic_close_circle); closeButton.setLayoutParams(new FrameLayout.LayoutParams(buttonHeight, buttonHeight, Gravity.TOP | Gravity.END)); // Logging Console adapter = new LoggingAdapter(context); listView = new ListView(context); listView.setBackgroundColor(Color.parseColor("#64000000")); listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); listView.setStackFromBottom(true); listView.setAdapter(adapter); FrameLayout.LayoutParams listViewLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); listViewLayoutParams.topMargin = buttonHeight / 2; listView.setLayoutParams(listViewLayoutParams); // Add views addView(listView); addView(closeButton); // Set View parameters WindowManager.LayoutParams windowParams; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { windowParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, layoutHeight, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); } else { windowParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, layoutHeight, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); } windowParams.gravity = Gravity.TOP | Gravity.START; windowParams.x = 0; windowParams.y = windowDimen.y - layoutHeight; // Attach and display View windowManager.addView(this, windowParams); }
Example 19
Source File: ToastHelper.java From ToastUtils with Apache License 2.0 | 4 votes |
/*** * 显示吐司弹窗 */ void show() { if (!isShow()) { /* 这里解释一下,为什么不复用 WindowManager.LayoutParams 这个对象 因为如果复用了,不同 Activity 之间不能共用一个,第一个 Activity 调用显示方法可以显示出来,但是会导致后面的 Activity 都显示不出来 又或者说,非第一次调用显示方法的 Activity 都会把这个显示请求推送给之前第一个调用显示的 Activity 上面,如果第一个 Activity 已经销毁,还会报以下异常 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@ef1ccb6 is not valid; is your activity running? */ final WindowManager.LayoutParams params = new WindowManager.LayoutParams(); /* // 为什么不能加 TYPE_TOAST,因为通知权限在关闭后设置显示的类型为 Toast 会报错 // android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? params.type = WindowManager.LayoutParams.TYPE_TOAST; */ /* // 这个是旧版本的写法,新版本已经废弃,因为 Activity onPause 方法被调用后这里把 Toast 取消显示了,这样做的原因:防止内存泄露 // 判断是否为 Android 6.0 及以上系统并且有悬浮窗权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(mToast.getView().getContext())) { // 解决使用 WindowManager 创建的 Toast 只能显示在当前 Activity 的问题 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else { params.type = WindowManager.LayoutParams.TYPE_PHONE; } } */ params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.format = PixelFormat.TRANSLUCENT; params.windowAnimations = android.R.style.Animation_Toast; params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; params.packageName = mPackageName; // 重新初始化位置 params.gravity = mToast.getGravity(); params.x = mToast.getXOffset(); params.y = mToast.getYOffset(); try { Activity topActivity = mWindowHelper.getTopActivity(); if (topActivity != null && !topActivity.isFinishing()) { WindowManager windowManager = (WindowManager) topActivity.getSystemService(Context.WINDOW_SERVICE); if (windowManager != null) { windowManager.addView(mToast.getView(), params); } } // 添加一个移除吐司的任务 sendEmptyMessageDelayed(hashCode(), mToast.getDuration() == Toast.LENGTH_LONG ? IToastStrategy.LONG_DURATION_TIMEOUT : IToastStrategy.SHORT_DURATION_TIMEOUT); // 当前已经显示 setShow(true); } catch (IllegalStateException | WindowManager.BadTokenException ignored) { // 如果这个 View 对象被重复添加到 WindowManager 则会抛出异常 // java.lang.IllegalStateException: View android.widget.TextView has already been added to the window manager. // 如果 WindowManager 绑定的 Activity 已经销毁,则会抛出异常 // android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@ef1ccb6 is not valid; is your activity running? } } }
Example 20
Source File: KcaLandAirBasePopupService.java From kcanotify with GNU General Public License v3.0 | 4 votes |
@Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(getApplicationContext())) { // Can not draw overlays: pass stopSelf(); } else { active = true; clickcount = 0; dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION); deckInfoCalc = new KcaDeckInfo(getApplicationContext(), getBaseContext()); KcaApiData.setDBHelper(dbHelper); setDefaultGameData(); LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); mView = mInflater.inflate(R.layout.view_labinfo_view, null); itemView = mInflater.inflate(R.layout.view_battleview_items, null); mView.setOnTouchListener(mViewTouchListener); mView.findViewById(R.id.view_lab_head).setOnTouchListener(mViewTouchListener); mView.setVisibility(View.GONE); ((TextView) mView.findViewById(R.id.view_lab_title)).setText(getStringWithLocale(R.string.viewmenu_airbase_title)); mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); popupWidth = mView.getMeasuredWidth(); popupHeight = mView.getMeasuredHeight(); // Button (Fairy) Settings mParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, getWindowLayoutType(), WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); mParams.gravity = Gravity.TOP | Gravity.START; Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Point size = new Point(); display.getSize(size); screenWidth = size.x; screenHeight = size.y; Log.e("KCA", "w/h: " + String.valueOf(screenWidth) + " " + String.valueOf(screenHeight)); mParams.x = (screenWidth - popupWidth) / 2; mParams.y = (screenHeight - popupHeight) / 2; mManager = (WindowManager) getSystemService(WINDOW_SERVICE); mManager.addView(mView, mParams); itemViewParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, getWindowLayoutType(), WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); } }