奥利奥(Oreo):如何从后台捕获位置更新? [英] Oreo: how to catch location update from the background?

查看:149
本文介绍了奥利奥(Oreo):如何从后台捕获位置更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的应用程序迁移到了oreo(因为Google Play现在要求),并且在启动手机时出现错误:

I migrate my app to oreo (as it's requiered by google play now), and i have now an error when i start my phone :


不允许启动服务意图

Not allowed to start service Intent

我的应用在后台监听任何位置更新,并定期将这些获取的位置发送到服务器。为此我要做:

My app is listening in background any location update and send periodically those grabbed locations to a server. for this i do :

AndroidManifest.xml

  <receiver android:name="com.myapp.StartServiceBroadcastReceiver">  
    <intent-filter>  
      <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
  </receiver> 

  @Override
  public void onReceive(Context context, Intent intent) {

    try {

        /* start the location service */  
        Intent startServiceIntent = new Intent(context, LocationService.class);
        context.startService(startServiceIntent);

    } catch (Throwable e){ Log.e(TAG, "Exception", e); }  

  }

和LocationService基本运行:

and the LocationService basiqually do :

public void onCreate() {

  mLocationServices.setListener(this);
  mLocationServices.startLocationUpdates(true, // startWithLastKnownLocation,

                                         150000, // interval => 2.5 min  // Set the desired interval for active location updates, in milliseconds.
                                                                         // The location client will actively try to obtain location updates for your
                                                                         // application at this interval, so it has a direct influence on the amount
                                                                         // of power used by your application. Choose your interval wisely.

                                         30000, // fastestInterval => 30 s  // Explicitly set the fastest interval for location updates, in milliseconds.
                                                                            // This controls the fastest rate at which your application will receive location updates, which might be faster than setInterval(long)
                                                                            // in some situations (for example, if other applications are triggering location updates).
                                                                            // This allows your application to passively acquire locations at a rate faster than it actively acquires locations, saving power.

                                         900000, // maxWaitTime => 15 min  // Sets the maximum wait time in milliseconds for location updates.
                                                                           // If you pass a value at least 2x larger than the interval specified with setInterval(long), then location
                                                                           // delivery may be delayed and multiple locations can be delivered at once. Locations are determined at
                                                                           // the setInterval(long) rate, but can be delivered in batch after the interval you set in this method.
                                                                           // This can consume less battery and give more accurate locations, depending on the device's hardware
                                                                           // capabilities. You should set this value to be as large as possible for your needs if you don't
                                                                           // need immediate location delivery.

                                         LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, // priority => Block level accuracy is considered to be about 100 meter accuracy.
                                                                                           //             Using a coarse accuracy such as this often consumes less power.

                                         25); // smallestDisplacement => 25 meters // Set the minimum displacement between location updates in meters  


}

@Override
public void onLocationChanged(Location location) {
   ....
}

在前色情电影中一切工作都很好,但是现在在oreo +上失败。我该怎么做才能使我的服务运行?

Everything was working well on pre-oreo but now it's failed on oreo+. What i can do to make my service running ?

推荐答案

从Android Oreo开始,您不能简单地在应用程序处于后台时启动后台服务

Starting in Android Oreo you cannot simply start a background service while the app is in the background.

您可以像这样启动前台服务(Kotlin,但在Java中与此类似):

You can start your service as foreground service like that (Kotlin, but similar in Java):

val serviceIntent = Intent(context, LocationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(serviceIntent)
} else {
    context.startService(serviceIntent)
}

在您的服务中,请确保致电< a href = https://developer.android.com/reference/android/app/Service.html#startForeground(int,%20android.app.Notification) rel = nofollow noreferrer> startForeground 尽可能。这还将发出通知,以便用户知道您的应用程序在后台处于活动状态。

In your service make sure you call startForeground as soon as possible. This will also issue a notification so that the user knows your app is active in the background.

Anis BEN NSIR 在这些评论,您可能还会受到新的背景位置限制

As Anis BEN NSIR has pointed out in the comments, you will probably also be affected by the new background location limits.

有一个关于奥利奥(Oreo)背景排放限制的好文章

这篇关于奥利奥(Oreo):如何从后台捕获位置更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆