android - Android 手机锁定后互联网连接暂停

标签 android location alarmmanager wakelock

我正在构建应用程序,它可以在后台定期使用 NETWORK_PROVIDER 获取位置,而无需使用 GPS_PROVIDER。我正在使用 ALARM_SERVICEWakeLock(PARTIAL_WAKE_LOCK)。但我面临的问题是,一旦屏幕关闭,互联网连接就会断开。当我解锁手机时,我开始接收位置信息,但当屏幕关闭时,我无法接收位置信息。

是不是因为:

  1. 一旦屏幕关闭,互联网连接就会暂停,而且当我解锁屏幕时,我会收到数据使用情况的 USSD 代码消息,这是否意味着一旦屏幕关闭,我的互联网连接就会断开?
  2. 即使互联网连接已打开,但位置不会在后台更新,因为屏幕处于关闭状态。

我正在使用 GpsTracker 类从 here 获取位置并使用 AlarmManager 定期获取位置。同样在 LatLongBroadcastReceiver 类中,我正在获取一个位置。

Intent intent = new Intent(GPSlatlongActivity.this,
                    LatLongBroadcastReceiver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

pendingIntent = PendingIntent.getBroadcast(
        GPSlatlongActivity.this.getApplicationContext(), 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(),
        (AlarmManager.INTERVAL_FIFTEEN_MINUTES / 15),
        pendingIntent);

最佳答案

根据文档 WifiManager.WifiLock

Allows an application to keep the Wi-Fi radio awake. Normally the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released. Multiple applications may hold WifiLocks, and the radio will only be allowed to turn off when no WifiLocks are held in any application.

我猜你需要这把锁。如果设备连接到 WiFi,则获取此锁。但是请

Note that WifiLocks cannot override the user-level "Wi-Fi Enabled" setting, nor Airplane Mode. They simply keep the radio from turning off when Wi-Fi is already on but the device is idle.

关于android - Android 手机锁定后互联网连接暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015197/

相关文章:

android - Buildozer 包可用于安装或更新错误

android - 膨胀类 android.support.v7.widget.Toolbar 时出错

android - 从库中的图像访问位置数据

android - AppWidget每分钟更新一次

android - Flutter Android 将 alarmmanager 与通知相结合

Android-检查 lastKnownLocation 年龄的最佳方法

android - whatsApp 发送消息让用户在 ios 中编辑消息,在 android 中它没有

flutter-web:通过 Location 插件在网络上获取位置

android - 如果设备已经在 Geofence android 中,则停止初始触发

android - Phonegap Android AlarmManager 插件?