OnLocationChanged() 在后台不起作用
OnLocationChanged() Doesn't work in background
我正在使用以下代码获取用户的当前位置。当应用程序在前台运行时,它运行良好,但是当我尝试在后台更改模拟器的位置时,它没有给我位置。我能做什么?
class BackgroundLocation:Service(),LocationListener {
override fun onBind(intent: Intent?) = null
@SuppressLint("MissingPermission")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val locationListener = BackgroundLocation()
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10f,locationListener)
return START_STICKY
}
override fun onLocationChanged(location: Location) {
println(location.toString())
}
override fun onProviderDisabled(provider: String) {
}
override fun onProviderEnabled(provider: String) {
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
}
目前您正在使用后台服务。它在 运行 时工作,一旦应用程序进入后台,服务就会停止。
要在后台持续更新,您需要将服务更新为前台服务。
ForegroundService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
MainActivity.java
public void startService() {
Intent serviceIntent = new Intent(this, ForegroundService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
ContextCompat.startForegroundService(this, serviceIntent);
}
如果您是 运行 android 10 及以上版本的应用,您需要请求后台位置。
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
您可以在此处阅读有关后台位置、它的限制和前台服务的所有信息 - https://developer.android.com/about/versions/oreo/android-8.0-changes
https://developer.android.com/guide/components/services
我正在使用以下代码获取用户的当前位置。当应用程序在前台运行时,它运行良好,但是当我尝试在后台更改模拟器的位置时,它没有给我位置。我能做什么?
class BackgroundLocation:Service(),LocationListener {
override fun onBind(intent: Intent?) = null
@SuppressLint("MissingPermission")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val locationListener = BackgroundLocation()
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10f,locationListener)
return START_STICKY
}
override fun onLocationChanged(location: Location) {
println(location.toString())
}
override fun onProviderDisabled(provider: String) {
}
override fun onProviderEnabled(provider: String) {
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
}
目前您正在使用后台服务。它在 运行 时工作,一旦应用程序进入后台,服务就会停止。
要在后台持续更新,您需要将服务更新为前台服务。
ForegroundService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
MainActivity.java
public void startService() {
Intent serviceIntent = new Intent(this, ForegroundService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
ContextCompat.startForegroundService(this, serviceIntent);
}
如果您是 运行 android 10 及以上版本的应用,您需要请求后台位置。
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
您可以在此处阅读有关后台位置、它的限制和前台服务的所有信息 - https://developer.android.com/about/versions/oreo/android-8.0-changes
https://developer.android.com/guide/components/services