美文网首页
悬浮窗+Dialog中的坑

悬浮窗+Dialog中的坑

作者: BlackNeko | 来源:发表于2016-07-26 11:38 被阅读699次

功能需求:

点击悬浮窗中的按钮显示一个Dialog

踩坑之路:

先是Context

Dialog adDialog = new Dialog(context, R.style.DialogStyle);
adDialogView = LayoutInflater.from(context).inflate(R.layout.dialog,null);
adDialog.setContentView(adDialogView);
adDialog.show();

崩溃,log:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

换成ApplicationContext

Dialog adDialog = new Dialog(context.getApplicationContext(), R.style.DialogStyle);
adDialogView = LayoutInflater.from(context).inflate(R.layout.dialog,null);
adDialog.setContentView(adDialogView);
adDialog.show();

崩溃,log:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

换成Activity

Dialog adDialog = new Dialog(activity, R.style.DialogStyle);
adDialogView = LayoutInflater.from(context).inflate(R.layout.dialog,null);
adDialog.setContentView(adDialogView);
adDialog.show();

第一次启动APP,点击显示悬浮窗,成功!退出APP再次进入,点击悬浮窗,崩溃!log:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41e48918 is not valid; is your activity running?

添加 TYPE_SYSTEM_ALERT

在代码中添加:adDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

Dialog adDialog = new Dialog(context.getApplicationContext(), R.style.DialogStyle);
adDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
adDialogView = LayoutInflater.from(context).inflate(R.layout.dialog,null);
adDialog.setContentView(adDialogView);
adDialog.show();

并且在manifest中增加权限:

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

此时,无论是使用context或者activity还是context.getApplicationContext()都可以正常显示Dialog

相关文章

  • 悬浮窗+Dialog中的坑

    功能需求: 点击悬浮窗中的按钮显示一个Dialog 踩坑之路: 先是Context 崩溃,log:android....

  • PopubWindow

    一、简介: PopubWindow,悬浮窗,支持滑动的悬浮窗(也可称为弹窗),与Dialog不同的是AlteDia...

  • 机型阿坑2.0

    机型坑 Android 6.0 中,使用 SYSTEM_ALERT_WINDOW 绘制的悬浮窗不能含有 eleva...

  • Android使用WindowManger实现桌面悬浮窗

    如果想实现一个在桌面显示的悬浮窗,用Dialog、PopupWindow、Toast等已经不能实现了,他们基本都是...

  • android 悬浮窗

    安卓悬浮窗的书写,我们分为几个步骤: 1.添加悬浮窗权限 2.书写悬浮窗代码,搭建悬浮窗布局 3.判断悬浮窗权限是...

  • Android Dialog中EditText无法弹出输入法解决

    记录一下昨晚遇到的坑点,本来是想写个悬浮工具的,可是在悬浮窗中的EditText死都弹不出输入法,各种百度Goog...

  • Android开发踏过的坑

    Dialog中的遇到的坑 去掉dialog的标题的方法 要是alertdialog的话就直接不设置setTitle...

  • 悬浮框

    使用悬浮框 悬浮窗监听器 悬浮窗管理器

  • 悬浮窗的实现

    悬浮窗是当Activity切换的时候,悬浮窗不会消失实现步骤是: ①在Application中的监听Activit...

  • Android悬浮手电的简单实现,简单但是很实用的功能

    在配置文件中添加悬浮窗权限和手电所需要的权限 Activity中开启service悬浮窗用来控制,当手机没有手电功...

网友评论

      本文标题:悬浮窗+Dialog中的坑

      本文链接:https://www.haomeiwen.com/subject/ywynjttx.html