在奥利奥(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

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

问题描述

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

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

然后,当应用程序处于空闲模式(应用程序处于后台或终止状态)时,将在10秒钟后调用onDestroy方法.下面是我的代码.

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();
        }
    }
}

任何帮助将不胜感激.

解决方案

您可以尝试以下两种选择之一,也可以尝试两种结合使用,这些解决了我遇到的问题.

选项1

要使位置更新继续在后台运行,您必须使用 LocationServices API c1> ,如此处此处中阅读Android Oreo 8.0文档,您将可以使用此解决方案.

第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));
        }
        else {
            mainActivity.startForegroundService(new Intent(getContext(), GpsServices.class));
            mainActivity.startForegroundService(new Intent(getContext(), BluetoothService.class));
            mainActivity.startForegroundService(new Intent(getContext(), BackgroundApiService.class));
        }

步骤2:使用通知显示您的服务正在运行.在onCreate服务方法中添加以下代码行.

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

步骤3:当服务停止或销毁时,删除notification.

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

选项2 的一个问题是,它将一直显示notification,直到GpsService在运行 Android Oreo 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.

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

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