Android 空闲时运行应用程序

发布于 2024-12-10 12:23:25 字数 270 浏览 0 评论 0原文

我想知道是否有一种方法可以在手机空闲/锁定时继续运行我的应用程序(基本上每 X 秒刷新一次 URL)? (当屏幕关闭时)。我目前正在使用 WebView 来加载 URL,并使用 Runnable 来重新加载它。

当我尝试使用服务时,它不断给我带来问题 - webview 无法解决。我查了一下,没能解决。

我也想知道有没有办法在后台浏览URL。基本上,在不显示浏览器的情况下浏览 URL :D

任何帮助将不胜感激! 抱歉,如果这些问题很愚蠢,我才开始 Android 编程几天。

I was wondering if there is a way to keep running my application (which basically just refreshes a URL every X seconds) even when the phone idles/locks? (when the screen turns off). I'm currently using a WebView to load the URL, and a Runnable to reload it.

When I tried using a service, it keeps giving me a problem - webview cannot be resolved. I looked it up, and I couldn't get it fixed.

I would also like to know if there is a way to browse a URL in the background. Basically, browsing a URL without showing the browser :D

Any help would be much appreciated!!!
Sorry if these questions are dumb, I've only started Android programming for a few days.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-12-17 12:23:25

你不需要 webview 来访问 web 服务器,除非你想立即显示结果。
您可以使用内置的 http 客户端来实现此目的(通过服务工作,不使用用户界面)

You do not need a webview to poke web server, unless you like to display results imeediately.
You can use built in http client for this purpose ( works from service and does not vae user interfce)

时光倒影 2024-12-17 12:23:25

我认为 Timer 任务将解决您的问题。

 Timer timer=new Timer();
timer.schedule(new DelayedTask(),1, 1000);// it will repeat within 1000ms
private class DelayedTask extends TimerTask
{
    @Override
    public void run() {
        runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
                webview.loadUrl(url);
            } 
        });


    }

}

@Override
public void onDestroy()
{
    super.onDestroy();
    timer.cancel();
}

I think Timer task will solve your problem.

 Timer timer=new Timer();
timer.schedule(new DelayedTask(),1, 1000);// it will repeat within 1000ms
private class DelayedTask extends TimerTask
{
    @Override
    public void run() {
        runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
                webview.loadUrl(url);
            } 
        });


    }

}

@Override
public void onDestroy()
{
    super.onDestroy();
    timer.cancel();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文