从JavaScript在Android的应用程序科尔多瓦改变KeepScreenOn [英] changing KeepScreenOn from javascript in Android cordova app

查看:181
本文介绍了从JavaScript在Android的应用程序科尔多瓦改变KeepScreenOn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图控制从我的科尔多瓦应用程序屏幕超时。该应用程序播放视频和,而应用程序是在播放视频我要关闭屏幕超时。虽然视频暂停或者他们正在做别的东西,我想重新打开它。 如果我在OnCreate中设置KeepScreenOn标志,它工作正常,但是如果我从我的插件没有什么变化调用它。我曾经尝试都

  getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)。
 

  this.webView.setKeepScreenOn(真正的);
 

下面是我的插件code。

 包com.Kidobi.plugins;

进口org.apache.cordova.api.CallbackContext;
进口org.apache.cordova.api.CordovaPlugin;
进口org.json.JSONArray;
进口org.json.JSONException;

进口android.view.WindowManager;

公共类KeepScreenOn扩展CordovaPlugin {

@覆盖
公共布尔执行(串动,JSONArray的args,最终CallbackContext callbackContext)抛出JSONException {
    的System.out.println(我现在的插件);
    如果(action.equals(KeepScreenOn)){
        的System.out.println(KeepScreenOn);
        this.webView.setKeepScreenOn(真正的);
        //cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //callbackContext.success(action);
        返回true;
    }否则,如果(action.equals(CancelKeepScreenOn)){
        的System.out.println(CancelKeepScreenOn);
        this.webView.setKeepScreenOn(假);
           //cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //callbackContext.success(action);
        返回true;
    } 其他 {
        的System.out.println(未知);
        callbackContext.error(未知动作+动作);
        返回false;
    }
}

}
 

解决方案

我添加了一个插件,使用这种code到gihub。使用CLI安装 须藤科尔多瓦插件添加 https://github.com/leohenning/KeepScreenOnPlugin 这已经过测试,科尔多瓦3.1

它做的线程。需要在UI线程上运行。 <一href="http://cordova.apache.org/docs/en/2.8.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android" rel="nofollow">http://cordova.apache.org/docs/en/2.8.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

请参见上穿线

因此​​code的作品看起来像

 包com.MyPlug.plugins;

进口org.apache.cordova.api.CallbackContext;
进口org.apache.cordova.api.CordovaPlugin;
进口org.json.JSONArray;
进口org.json.JSONException;

进口android.view.WindowManager;

公共类KeepScreenOn扩展CordovaPlugin {

    @覆盖
    公共布尔执行(串动,JSONArray的args,最终CallbackContext callbackContext)抛出JSONException {
        的System.out.println(我现在的插件);
        如果(action.equalsIgnoreCase(KeepScreenOn)){
            的System.out.println(开始KeepScreenOn);
            cordova.getActivity()。runOnUiThread(新的Runnable(){
                公共无效的run(){
                    。cordova.getActivity()getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    的System.out.println(屏幕就会不停地在KeepScreenOn。);
                }
            });
            //cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //callbackContext.success(action);
            返回true;
        }否则,如果(action.equalsIgnoreCase(CancelKeepScreenOn)){
            的System.out.println(CancelKeepScreenOn);
            cordova.getActivity()。runOnUiThread(新的Runnable(){
                公共无效的run(){
                    。cordova.getActivity()getWindow()clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    的System.out.println(画面将不会被保留在取消KeepScreenOn。);
                }
            });
            //cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //callbackContext.success(action);
            返回true;
        } 其他 {
            的System.out.println(未知);
            callbackContext.error(未知动作+动作);
            返回false;
        }
    }

}
 

然后从我的JavaScript调用

  cordova.exec(NULL,NULL,KeepScreenOn,CancelKeepScreenOn,[]);
 

在config.xml

 &LT;功能名称=KeepScreenOn&GT;
  &LT; PARAM NAME =Android的包值=com.MyPlug.plugins.KeepScreenOn/&GT;
&LT; /功能&GT;
 

,非常感谢这个问题 <一href="http://stackoverflow.com/questions/14227152/android-phonegap-error-calling-method-on-npobject">Android &放大器; PhoneGap的 - 对NPObject 错误调用方法

I am trying to control the screen timeout from my cordova app. The app plays videos and while the app is playing a video I want to turn off the screen timeout. While a video is paused or they are doing something else I want to turn it back on. If I set the KeepScreenOn flag in OnCreate it works fine however if I call it from my plugin nothing changes. I have tried both

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

and

this.webView.setKeepScreenOn(true); 

Here is my plugin code.

package com.Kidobi.plugins;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.view.WindowManager;

public class KeepScreenOn extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    System.out.println("Im in the plugin");
    if (action.equals("KeepScreenOn")) {
        System.out.println("KeepScreenOn");
        this.webView.setKeepScreenOn(true);
        //cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //callbackContext.success(action);
        return true;
    } else if (action.equals("CancelKeepScreenOn")){
        System.out.println("CancelKeepScreenOn");
        this.webView.setKeepScreenOn(false);
           //cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //callbackContext.success(action);
        return true;
    } else {
        System.out.println("UNKNOWN");
        callbackContext.error("unknown action" + action);
        return false;
    }
}

}

解决方案

I have added a plugin to gihub using this code. to install it using the cli sudo cordova plugins add https://github.com/leohenning/KeepScreenOnPlugin this has been tested for cordova 3.1

It has to do with the thread. Need to be running on the UI thread. http://cordova.apache.org/docs/en/2.8.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

see the section on threading

so the code that works looks like

package com.MyPlug.plugins;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.view.WindowManager;

public class KeepScreenOn extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        System.out.println("Im in the plugin");
        if (action.equalsIgnoreCase("KeepScreenOn")) {
            System.out.println("Start KeepScreenOn");
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    System.out.println("Screen will be kept on. KeepScreenOn");
                }
            });
            //cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //callbackContext.success(action);
            return true;
        } else if (action.equalsIgnoreCase("CancelKeepScreenOn")){
            System.out.println("CancelKeepScreenOn");
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    System.out.println("Screen will not be kept on. Cancel KeepScreenOn");
                }
            });
            //cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //callbackContext.success(action);
            return true;
        } else {
            System.out.println("UNKNOWN");
            callbackContext.error("unknown action" + action);
            return false;
        }
    }

}

then from the javascript I call

cordova.exec(null, null, "KeepScreenOn", "CancelKeepScreenOn", [""]);

the config.xml

<feature name="KeepScreenOn">
  <param name="android-package" value="com.MyPlug.plugins.KeepScreenOn"/>
</feature>

with thanks to this problem Android & PhoneGap -- Error calling method on NPObject

这篇关于从JavaScript在Android的应用程序科尔多瓦改变KeepScreenOn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆