简体   繁体   English

为什么在使用cordova插件时“ setAttributes()”无法正常工作?

[英]Why the “setAttributes()” doesn't work properly while using cordova plugin?

I want to create a plugin that connect to javascript while using cordova for the plugin, this is my javascipt code : 我想创建一个插件,同时使用cordova作为插件,这是我的javascipt代码:

var Brightness = function() {};

Brightness.prototype.brightnessUp = function( success, error ) 
{
    return cordova.exec( success, error, "Brightness", "brightnessUp", [] );
};

and my plugin code : 和我的插件代码:

public PluginResult execute(String action, JSONArray data, String callbackId) {
    PluginResult result = null;
    if(action.equals(BRIGHTNESS_UP)){
        lightUP();
        result = new PluginResult(Status.OK);
    }

    return result;
}

public void lightUP(){
    WindowManager.LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes();
    layoutParams.screenBrightness = 1.0f;
    cordova.getActivity().getWindow().getAttributes().screenBrightness = 1.0f;
    cordova.getActivity().getWindow().setAttributes(layoutParams);
    cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.SCREEN_BRIGHTNESS_CHANGED);
}

Somehow I don't know why the "setAttributes(layoutParam)" doesn't make any changed to the brightness screen in my application. 不知何故,我的应用程序为何不对“ setAttributes(layoutParam)”进行任何更改。

Accidentally when I press the home button, and go back to the application, suddenly the screen brightness changed according to the layoutParams. 无意中,当我按下主页按钮并返回到应用程序时,屏幕亮度突然根据layoutParams改变了。

Is there any solution for this? 有什么解决办法吗?

What you have is correct up until a point. 到目前为止,您所拥有的都是正确的。 You'll need to start a dummy activity right after you set the brightness in order for the change to take affect. 设置亮度后,您需要立即启动虚拟活动,以使更改生效。 Take a look at this SO answer: 看看这个SO答案:

https://stackoverflow.com/a/7658364/41679 https://stackoverflow.com/a/7658364/41679

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM