我正在构建应用程序,它可以在后台定期使用 NETWORK_PROVIDER
获取位置,而无需使用 GPS_PROVIDER
。我正在使用 ALARM_SERVICE
和 WakeLock
(PARTIAL_WAKE_LOCK
)。但我面临的问题是,一旦屏幕关闭,互联网连接就会断开。当我解锁手机时,我开始接收位置信息,但当屏幕关闭时,我无法接收位置信息。
是不是因为:
- 一旦屏幕关闭,互联网连接就会暂停,而且当我解锁屏幕时,我会收到数据使用情况的 USSD 代码消息,这是否意味着一旦屏幕关闭,我的互联网连接就会断开?
- 即使互联网连接已打开,但位置不会在后台更新,因为屏幕处于关闭状态。
我正在使用 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/