【问题标题】:OnLocationChanged() Doesn't work in backgroundOnLocationChanged() 在后台不起作用
【发布时间】:2021-09-13 02:17:10
【问题描述】:

我正在使用以下代码来获取用户的当前位置。当应用程序在前台时,它运行良好,但是当我尝试在后台更改模拟器的位置时,它并没有给我位置。我能做什么?

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?) {
    
}

}

【问题讨论】:

    标签: android kotlin gps location maps


    【解决方案1】:

    目前您正在使用后台服务。它在运行时工作,一旦应用程序进入后台,服务就会停止。
    要在后台持续更新,您需要将服务更新为前台服务。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多