在 Oreo (8.0.0+) (API 26+) 中,如何在应用程序处于后台或终止时获取位置服务更新 [英] In Oreo (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill

查看:24
本文介绍了在 Oreo (8.0.0+) (API 26+) 中,如何在应用程序处于后台或终止时获取位置服务更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android O (8.0.0+) (API 26+) 中,如何在应用处于后台或终止时获取位置服务更新.在Strava"Android 应用中(可在 Play 商店),定位服务在应用程序在后台或kill时正常运行.我需要在我的应用程序中构建相同的功能.每当我开始使用服务时

startService(new Intent(this, GpsServices.class));

然后当应用处于空闲模式(应用在后台或被杀死)时,在 10 秒后调用 onDestroy 方法.下面是我的代码.

public class GpsServices extends Service implements LocationListener, Listener {@覆盖公共无效 onCreate() {mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {返回;}如果(mLocationManager!= null){mLocationManager.addGpsStatusListener(this);}如果(mLocationManager!= null){mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,500,0,这);}}公共无效onLocationChanged(位置位置){System.out.println("位置 = [" + 位置 + "]");}@覆盖public int onStartCommand(Intent intent, int flags, int startId) {//如果我们被杀死,从这里返回后,重新启动返回 START_STICKY;}@覆盖公共IBinder onBind(意图意图){//我们不提供绑定,所以返回 null返回空值;}/* 在服务停止时删除 locationlistener 更新 */@覆盖公共无效 onDestroy() {尝试 {mLLocationManager.removeUpdates(this);mLocationManager.removeGpsStatusListener(this);停止前景(真);} 捕捉(异常 e){e.printStackTrace();}}}

任何帮助将不胜感激.

解决方案

您可以尝试以下两个选项之一或两者的组合,当我遇到这些问题时,它们已经解决了我的问题.

选项 1

要在后台继续运行位置更新,您必须使用 LocationServices APIFusedLocationProviderClienthere 所述此处 在文档或 此处在 CODEPATH 中.

选项 2

如果您在 这里,你会得到这个解决方案.

<块引用>

第 1 步:确保将服务作为前台服务启动,如下面的代码所示

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {mainActivity.startService(new Intent(getContext(), GpsServices.class));mainActivity.startService(new Intent(getContext(), BluetoothService.class));mainActivity.startService(new Intent(getContext(), BackgroundApiService.class));}别的 {mainActivity.startForegroundService(new Intent(getContext(), GpsServices.class));mainActivity.startForegroundService(new Intent(getContext(), BluetoothService.class));mainActivity.startForegroundService(new Intent(getContext(), BackgroundApiService.class));}

<块引用>

第 2 步:使用通知来表明您的服务正在运行.在服务的 onCreate 方法中添加以下代码行.

@Override公共无效 onCreate() {...if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForeground(NOTIFICATION_ID, 通知);}...}

<块引用>

第三步:在服务停止或销毁时移除通知.

@Override公共无效 onDestroy() {...if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {停止前景(真);//true 将删除通知}...}

选项 2 的一个问题是它会一直显示 通知,直到您的 GpsService 在所有运行在 上的设备上运行Android 奥利奥 8.0.

我确信即使应用程序处于后台或终止状态,这两个选项都可以工作.

我希望这个解决方案可以解决您的问题.

In Android O (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill. In "Strava" Android App (Available on Play Store), location services run properly when the app is in the background or kill. I need to build same functionality in my app. Whenever I start service using

startService(new Intent(this, GpsServices.class));

Then the onDestroy method is called after 10 sec when an app is an idle mode (the app is in the background or kill). Below is my code.

public class GpsServices extends Service implements LocationListener, Listener {

    @Override
    public void onCreate() {
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (mLocationManager != null) {
            mLocationManager.addGpsStatusListener(this);
        }
        if (mLocationManager != null) {
            mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                500,
                0,
                this);
        }    
    }

    public void onLocationChanged(Location location) {
        System.out.println("location = [" + location + "]");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // If we get killed, after returning from here, restart
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    /* Remove the locationlistener updates when Services is stopped */
    @Override
    public void onDestroy() {
        try {
            mLocationManager.removeUpdates(this);
            mLocationManager.removeGpsStatusListener(this);
            stopForeground(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Any help would be appreciated.

解决方案

You can try one of below two options or a combination of both, which have solved my problems when I have faced them.

Option 1

For location update to continue running in the background, you must use LocationServices API with FusedLocationProviderClient as described here and here in docs or here in CODEPATH.

Option 2

If you would have read the Android Oreo 8.0 Documentation properly somewhere in here, you would have landed on this solution.

Step 1: Make sure you start a service as a foreground service as given in below code

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {

            mainActivity.startService(new Intent(getContext(), GpsServices.class));
            mainActivity.startService(new Intent(getContext(), BluetoothService.class));
            mainActivity.startService(new Intent(getContext(), BackgroundApiService.class));
        }
        else {
            mainActivity.startForegroundService(new Intent(getContext(), GpsServices.class));
            mainActivity.startForegroundService(new Intent(getContext(), BluetoothService.class));
            mainActivity.startForegroundService(new Intent(getContext(), BackgroundApiService.class));
        }

Step 2: Use notification to show that your service is running. Add below line of code in onCreate method of service.

@Override
public void onCreate() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(NOTIFICATION_ID, notification);
    }
    ...
}

Step 3: Remove the notification when the service is stopped or destroyed.

@Override
public void onDestroy() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          stopForeground(true); //true will remove notification
    }
    ...
}

One problem with Option 2 is that it will keep showing the notification until your GpsService is running on all devices running on Android Oreo 8.0.

I'm sure that both these options will work even when the app is in the background or in kill state.

I hope this solution might solve your problem.

这篇关于在 Oreo (8.0.0+) (API 26+) 中,如何在应用程序处于后台或终止时获取位置服务更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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