通过 luajava 获取 Android 系统设置
Posted
技术标签:
【中文标题】通过 luajava 获取 Android 系统设置【英文标题】:Get Android System Setting via luajava 【发布时间】:2019-06-27 15:11:58 【问题描述】:我正在尝试在 XPrivacyLua 自定义挂钩中获取系统设置的值。
Settings.Secure | android Developers #getInt()
function after(hook, param)
local result = param:getResult()
if result == null or result:getItemCount() == 0 then
return false
end
--
local context = param:getApplicationContext()
local cls = luajava.bindClass('android.provider.Settings$Secure')
local isColorInverted = cls:getInt(context, cls:ACCESSIBILITY_DISPLAY_INVERSION_ENABLED)
if isColorInverted == 1 then
return true
end
--
local fake = result:newPlainText('XPrivacyLua', 'Private')
param:setResult(fake)
return true
end
尝试 1: cls:
ACCESSIBILITY_DISPLAY_INVERSION_ENABLED
local isColorInverted = cls:getInt(context, cls:ACCESSIBILITY_DISPLAY_INVERSION_ENABLED)
-- [string "script"]:9: function arguments expected
尝试 2: cls.
ACCESSIBILITY_DISPLAY_INVERSION_ENABLED
local isColorInverted = cls:getInt(context, cls.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED)
-- Exception:
-- org.luaj.vm2.LuaError: script:9 no coercible public method at org.luaj.vm2.LuaValue.error(SourceFile:1041)
-- ...
-- <full stack trace>
尝试 3: ACCESSIBILITY_DISPLAY_INVERSION_ENABLED
local isColorInverted = cls:getInt(context, ACCESSIBILITY_DISPLAY_INVERSION_ENABLED)
-- Same as attempt 2
luajava 中获取ACCESSIBILITY_DISPLAY_INVERSION_ENABLED
值的正确语法是什么?
【问题讨论】:
【参考方案1】:我给getInt
的第一个参数是错误的。
它调用了一个 ContentResolver,我给它传递了一个 ApplicationContext。
下面是工作代码。
function after(hook, param)
local result = param:getResult()
if result == null or result:getItemCount() == 0 then
return false
end
--
local context = param:getApplicationContext()
local cr = context:getContentResolver()
local cls = luajava.bindClass('android.provider.Settings$Secure')
local isColorInverted = cls:getInt(cr, ACCESSIBILITY_DISPLAY_INVERSION_ENABLED)
if isColorInverted == 1 then
return true
end
--
local fake = result:newPlainText('XPrivacyLua', 'Private')
param:setResult(fake)
return true
end
【讨论】:
以上是关于通过 luajava 获取 Android 系统设置的主要内容,如果未能解决你的问题,请参考以下文章