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

使用通知,您可以使您的服务生动.它适用于

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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值