如果您想使用 activity.stopLockTask() 方法来停止锁定任务,但是遇到了 SecurityException 异常,您可以尝试使用以下代码:
try { stopLockTask(); } catch (SecurityException e) { String msg = e.getMessage(); if (msg != null && msg.contains("not in lock task mode")) { // Do nothing - This device was not previously locked } else { throw e; } }
在上面的代码中,我们使用 try-catch 块来捕获 SecurityException 异常。如果异常消息包含“not in lock task mode”字符串,则表示该设备以前未被锁定。否则,我们仍然会抛出异常。这种方法可以让您在安全地停止锁定任务时避免异常。