简体   繁体   中英

Run Javascript Webapp in Background of native App

i'm working on an android app, which displays a webapp/website in its main activitiy like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

      WebView myWebView = (WebView) findViewById(R.id.webview);
      WebSettings webSettings = myWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      myWebView.setWebViewClient(new WebViewClient());
      myWebView.loadUrl("http://localhost");    
}

My webApp/Website, i'd like to open, uses some javascript code, which has to be executed a long time.

But now here's the problem: I want to run this javascript also when the screen turns off after some seconds or anything else happens. Ideally, I want to open my Android App, which opens my webapp and execute the js-code, whether my phone is locked, display turned off etc..

First I thought about a Service, but I don't know if this makes sense while displaying a website.

I hope someone could help me or give me a hint.

Regards

Depending on your specific use-case, it might be possible to use Javascript interpreter such as Mozilla Rhino . It's not a browser, just a headless runtime, so it can be run from within a Service (in fact, I'm using it in one of my projects in this exact way).

The problem here would be to pass a job from your WebView to Rhino - those are two separate runtimes. Both Rhino and WebView allow you to bind Java to Javascript, so it could be accomplished, but it won't be straigthforward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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