Android:如何从 AlarmManager 唤醒 WIFI(或 3G)?

发布于 2025-01-03 00:14:52 字数 190 浏览 1 评论 0原文

我有这个应用程序需要每 30 分钟从服务器提取数据,经过大量搜索后我找到了这个解决方案: 1- 使用 AlarmManager 每 30 分钟通知应用程序一次,而不是让服务在后台运行。 2-使用带有 PARTIAL_WAKE_LOCK 的唤醒锁

我唯一的问题是 Wifi 在睡眠时关闭。

当我的闹钟被触发时,我如何请求需要互联网连接?

I have this application that needs to pull data from a server every 30 minutes, after a lot of search I reached this solution:
1- using AlarmManager to notify the application each 30 minutes instead of keeping the service running in the background.
2- using wakelocks with PARTIAL_WAKE_LOCK

My only problem is that Wifi is off at sleep time.

How can I request that I need an internet connection when my alarm is triggered??

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

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

发布评论

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

评论(3

幸福丶如此 2025-01-10 00:14:52

前段时间我也遇到了完全相同的问题。不幸的是我没有成功。我尝试了以下方法:

    WifiManager wman = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);

    try {
        wman.reconnect();
    } catch (Exception e) {
        e.printStackTrace();
        return false;

    }

也许您可以使用 WifiManager并找到一种我没有找到的方法。哦,是的,别忘了在清单中设置 WIFI 权限。

干杯卢卡斯

I had exactly the same problem some time ago. Unfortunately I did not succeed. I tried the following:

    WifiManager wman = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);

    try {
        wman.reconnect();
    } catch (Exception e) {
        e.printStackTrace();
        return false;

    }

Maybe you can play around with the WifiManager and find a way that I didn't find. Oh yea, don't forget to set the WIFI permissions in the manifest.

Cheers Lukas

请叫√我孤独 2025-01-10 00:14:52
  1. 警报管理器通过接收器唤醒您的唤醒意图服务
  2. 唤醒意图服务获取 wifi 锁定
  3. 服务注册接收器以进行连接并调用 reconnect()reassociate() 以及所需的任何内容(这可能是特定于设备的)
  4. 服务在锁存器上等待,
  5. 当 wifi 连接时接收器唤醒服务

骨架代码:https://stackoverflow.com/a/19968708/281545

  1. alarm manager wakes your wakeful intent service via receiver
  2. wakeful intent service acquires a wifi lock
  3. service registers a receiver for connectivity and calls reconnect(), reassociate() and whatever is needed (this may be device specific)
  4. service waits on a latch
  5. the receiver wakes the service up when the wifi is connected

Skeleton code : https://stackoverflow.com/a/19968708/281545

烟燃烟灭 2025-01-10 00:14:52

Android 6(API 级别 23)及更高版本的注意事项。
自从 Google 推出了针对打瞌睡和应用程序待机进行优化以来,事情变得更加复杂。看这里——
https://developer.android.com/training/monitoring-设备状态/doze-standby.html
现在,强制在特定时间访问网络要困难得多。

Note for Android 6 (API level 23) and above.
Since Google introduced Optimizing for Doze and App Standby, things are getting more complicated. Look here -
https://developer.android.com/training/monitoring-device-state/doze-standby.html
Now it is much harder to force access to the network at a specific time.

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