关于 8.0以上WindowManager addView 报错 异常关于type类型设置

本文介绍了解决在Android 8.0及以上版本中使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT时出现的BadTokenException异常的方法。通过判断Android版本并调整LayoutParams类型,确保应用在不同版本的Android系统上正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8.0版本如果设置
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;会报如下异常
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@d4c6880 -- permission denied for window type 2003
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:909)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:372)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:136)
解决办法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else {
    layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
var LogFloatWindow = (function () { function LogFloatWindow(y) { this.x = 0; this.y = y || device.height * 0.5; this.workThread = null; this.storage = $storages.create('floatLogWindow'); this.onStart = null; this.onStop = null; } LogFloatWindow.prototype.init = function () { var _this = this; this.floatyLogW = $floaty.rawWindow( '<card id="root" h="150" cardBackgroundColor="#b3000000" cardElevation="0" cardCornerRadius="0" gravity="center_vertical">\n <vertical id="layout" marginLeft="22">\n <horizontal w="*" margin="5">\n <horizontal h="auto" w="0" layout_weight="1">\n <text id="title" text="" textSize="13dp" textColor="#FFD700" textStyle="bold" maxLines="1" ellipsize="end" style="Widget/AppCompat.Button.Borderless" />\n <text id="label" text="" textSize="13dp" textColor="#1E90FF" textStyle="bold" maxLines="1" ellipsize="end" style="Widget/AppCompat.Button.Borderless" marginLeft="10" />\n </horizontal>\n <Chronometer id="runTime" textSize="13dp" textColor="#03D96A" style="Widget/AppCompat.Button.Borderless" textStyle="bold" />\n </horizontal>\n <button gravity="center_horizontal" textColor="#DC143C" bg="#FFF5EE" h="1" padding="0" margin="0" textStyle="bold" />\n <console id="console" w="*" h="*" padding="5" />\n </vertical>\n </card>' ); this.controller = floaty.rawWindow( '<vertical>\n <vertical h="150" id="main">\n <text layout_weight="5" id="start" textStyle="bold" gravity="center" w="22dp" text="\u542F\u52A8" textColor="#333333" bg="#00a86b" />\n <text layout_weight="5" id="hideLog" textStyle="bold" gravity="center" w="22dp" text="\u65E5\u5FD7" textColor="#333333" bg="#FF8E12" />\n <text layout_weight="5" id="stop" textStyle="bold" gravity="center" w="22dp" text="\u7ED3\u675F" textColor="#333333" bg="#ee4d2d" />\n </vertical>\n </vertical>' ); ui.post(function () { setTimeout(function () { if (_this.floatyLogW && _this.controller) { try { _this.logButton = _this.controller.hideLog; _this.startButton = _this.controller.start; _this.stopButton = _this.controller.stop; _this.runTimeView = _this.floatyLogW.runTime; if (_this.floatyLogW.main) { _this.floatyLogW.main.getRootView().setAlpha(0.8); } _this.floatyLogW.setTouchable(false); _this.runTimeView.setFormat('运行时长 %s '); _this.runTimeView.setBase(android.os.SystemClock.elapsedRealtime()); var floatConsole = _this.floatyLogW.console; floatConsole.setConsole(runtime.console); floatConsole.setInputEnabled(false); floatConsole.setColor('V', '#ffff00'); floatConsole.setColor('I', '#ffffff'); floatConsole.setColor('D', '#ffff00'); floatConsole.setColor('W', '#673ab7'); floatConsole.setColor('E', '#ff0000'); floatConsole.setTextSize(13); _this.floatyLogW.setSize(-1, -2); var storedX = _this.storage.get('floatLogWindowX', _this.x); var storedY = _this.storage.get('floatLogWindowY', _this.y); _this.floatyLogW.setPosition(storedX, storedY); _this.controller.setPosition(storedX, storedY); _this.logButton.setText('隐藏'); _this.logButton.attr('background', '#d1c7b7'); if (_this.startButton) { _this.startButton.setOnClickListener(new java.lang.Object({ onClick: function () { _this.handleStart(_this); } })); } if (_this.stopButton) { _this.stopButton.setOnClickListener(new java.lang.Object({ onClick: function () { _this.handleStop(_this); } })); } if (_this.logButton) { _this.logButton.setOnClickListener(new java.lang.Object({ onClick: function () { _this.handleLog(_this); } })); } } catch (error) { console.error("初始化悬浮窗时出错:", error); } } }, 300); }); }; LogFloatWindow.getInstance = function () { if (!LogFloatWindow.instance) { LogFloatWindow.instance = new LogFloatWindow(); } return LogFloatWindow.instance; }; LogFloatWindow.prototype.handleStop = function (_this) { $app.launchPackage(context.getPackageName()); toast('停止本次任务'); if (_this.workThread) { _this.workThread.interrupt(); } _this.floatyLogW.close(); _this.controller.close(); $threads.shutDownAll(); }; LogFloatWindow.prototype.handleLog = function (_this) { var x = _this.storage.get('floatLogWindowX', 0); var y = _this.storage.get('floatLogWindowY', device.height * 0.5); ui.post(function () { if (_this.logButton.attr('text') == '隐藏') { _this.floatyLogW.setPosition(3000, 3000); _this.logButton.attr('text', '日志'); _this.logButton.attr('background', '#FF8E12'); } else { _this.logButton.setText('隐藏'); _this.logButton.attr('background', '#d1c7b7'); _this.floatyLogW.setPosition(x, y); } }); }; LogFloatWindow.prototype.handleStart = function (_this) { toast('开始本次任务'); if (!_this.workThread || !_this.workThread.isAlive()) { if (_this.controller) _this.toggleStatus(true); _this.workThread = threads.start(function () { try { _this.info('任务开始'); _this.onStart && _this.onStart(); if (_this.controller) _this.toggleStatus(false); _this.info('任务完成'); } catch (e) { var info = $debug.getStackTrace(e); if (info.includes('com.stardust.autojs.runtime.exception.ScriptInterruptedException')) { _this.info('停止任务'); } else { _this.error('任务报错: ' + e); _this.error('程序运行失败: ' + info); } _this.toggleStatus(false); } }); } else { _this.onStop && _this.onStop(); _this.toggleStatus(false); _this.workThread && _this.workThread.interrupt(); } }; LogFloatWindow.prototype.dateFormat = function (date, fmt_str) { return new java.text.SimpleDateFormat(fmt_str).format(date || new Date()); }; LogFloatWindow.prototype.info = function (msg) { console.info('['.concat(this.dateFormat(new Date(), 'HH:mm:ss'), ']').concat(msg)); }; LogFloatWindow.prototype.error = function (msg) { console.error('['.concat(this.dateFormat(new Date(), 'HH:mm:ss'), ']').concat(msg)); }; LogFloatWindow.prototype.setFloatyTitle = function (title) { ui.run(function () { if (this.floatyLogW && this.floatyLogW.title) { this.floatyLogW.title.attr('text', title); } }); }; LogFloatWindow.prototype.setFloatySubTitle = function (label) { ui.run(function () { if (this.floatyLogW && this.floatyLogW.label) { this.floatyLogW.label.attr('text', label); } }); }; LogFloatWindow.prototype.toggleStatus = function (enabled) { $ui.post(function () { if (this.floatyLogW && this.controller && this.runTimeView && this.startButton) { if (enabled) { this.runTimeView.setBase(android.os.SystemClock.elapsedRealtime()); this.runTimeView.start(); this.startButton.setText('停止'); this.startButton.attr('background', '#05a7f4'); } else { this.runTimeView.stop(); this.startButton.setText('启动'); this.startButton.attr('background', '#00a86b'); } } }); }; return LogFloatWindow; })(); var logWindow = LogFloatWindow.getInstance(); logWindow.init(); logWindow.setFloatyTitle("任务日志"); logWindow.setFloatySubTitle("运行状态"); logWindow.onStart = function () { logWindow.info("任务已启动"); for (let i = 0; i < 5; i++) { logWindow.info("执行第 " + (i + 1) + " 次任务"); threads.sleep(1000); // 模拟任务延迟 } }; logWindow.handleStart(logWindow); setTimeout(function () { logWindow.handleStop(logWindow); }, 3000000); AutoJsPro,以上代码哪出错了,把代码结合在一起给我
05-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值