其他分享
首页 > 其他分享> > android – 持续在后台服务中获取LocationUpdates

android – 持续在后台服务中获取LocationUpdates

作者:互联网

我正在处理需要在后台服务中连续获取位置更新的应用程序.我使用了它正在使用的后台粘性服务.但是,即使我已经添加了启动广播并在那里启动了服务,服务也不会在启动完成后启动.服务开始并立即被杀死.

此外,这不适用于奥利奥.应用程序关闭几分钟后服务停止,直到应用程序重新启动后才会重新启动.

我已经浏览了很多链接,博客建议使用AlarmManager / JobScheduler / JobIntentService,但没有得到满意的解决方案.
因此,请建议即使在启动后也可用于在后台持续获取位置的工作策略/解决方案,并且应该在Oreo上工作.

解决方法:

使用通知,您可以使您的服务生动.它适用于Android 8.1.
以下是后台服务的代码

注意:

1)对上面的Build.VERSION_CODES.O使用startForegroundService

2)使用targetSdkVersion 25

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

BackgroundGpsServices类

public class BackgroundGpsServices extends Service implements LocationListener {
private LocationManager mLocationManager;
public final long UPDATE_INTERVAL = 500;  /* 0.5 sec */
public static final int NOTIFICATION_ID = 200;

@Override
public void onCreate() {
    sendNotification(this, false);
    startLocationUpdates();
}

private void startLocationUpdates() {
    if (!isLocationUpdateRunning) {
        isLocationUpdateRunning = true;
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (mLocationManager != null) {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, UPDATE_INTERVAL, 0, this);
        }
    }
}

@Override
public void onLocationChanged(Location location) {
    sendNotification(BackgroundGpsServices.this, true);
    System.out.println("onLocationChanged ----- location=" + location);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {
}

public static void sendNotification(Service service, boolean isUpdate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Intent intent = new Intent(service, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, intent, PendingIntent.FLAG_NO_CREATE);
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(service)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("INFO_NOTIFICATION_TITLE")
                .setOngoing(true)
                .setAutoCancel(false)
                .setContentText("INFO_NOTIFICATION_MESSAGE")
                .setContentIntent(pendingIntent);
        Notification notification = mNotifyBuilder.build();

        if (isUpdate) {
            NotificationManager notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.notify(NOTIFICATION_ID, notification);
            }
        } else {
            service.startForeground(NOTIFICATION_ID, notification);
        }
    }
}

@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 {
        stopLocationUpdates();
        stopForeground(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void stopLocationUpdates() {
    isLocationUpdateRunning = false;
    if (mLocationManager != null) {
        mLocationManager.removeUpdates(this);
    }
}
}

标签:android,location,background-service
来源: https://codeday.me/bug/20190522/1152811.html