Keep wake lock for service
Этот коммит содержится в:
@@ -18,7 +18,6 @@ package org.traccar.client;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.PowerManager;
|
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -41,18 +40,6 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
private DatabaseHelper databaseHelper;
|
private DatabaseHelper databaseHelper;
|
||||||
private NetworkManager networkManager;
|
private NetworkManager networkManager;
|
||||||
|
|
||||||
private PowerManager.WakeLock wakeLock;
|
|
||||||
|
|
||||||
private void lock() {
|
|
||||||
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void unlock() {
|
|
||||||
if (wakeLock.isHeld()) {
|
|
||||||
wakeLock.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TrackingController(Context context) {
|
public TrackingController(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
handler = new Handler();
|
handler = new Handler();
|
||||||
@@ -63,9 +50,6 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
isOnline = networkManager.isOnline();
|
isOnline = networkManager.isOnline();
|
||||||
|
|
||||||
url = preferences.getString(MainFragment.KEY_URL, context.getString(R.string.settings_url_default_value));
|
url = preferences.getString(MainFragment.KEY_URL, context.getString(R.string.settings_url_default_value));
|
||||||
|
|
||||||
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
||||||
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -133,7 +117,6 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
|
|
||||||
private void write(Position position) {
|
private void write(Position position) {
|
||||||
log("write", position);
|
log("write", position);
|
||||||
lock();
|
|
||||||
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(boolean success, Void result) {
|
public void onComplete(boolean success, Void result) {
|
||||||
@@ -143,14 +126,12 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
isWaiting = false;
|
isWaiting = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void read() {
|
private void read() {
|
||||||
log("read", null);
|
log("read", null);
|
||||||
lock();
|
|
||||||
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(boolean success, Position result) {
|
public void onComplete(boolean success, Position result) {
|
||||||
@@ -167,14 +148,12 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
} else {
|
} else {
|
||||||
retry();
|
retry();
|
||||||
}
|
}
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void delete(Position position) {
|
private void delete(Position position) {
|
||||||
log("delete", position);
|
log("delete", position);
|
||||||
lock();
|
|
||||||
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(boolean success, Void result) {
|
public void onComplete(boolean success, Void result) {
|
||||||
@@ -183,14 +162,12 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
} else {
|
} else {
|
||||||
retry();
|
retry();
|
||||||
}
|
}
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send(final Position position) {
|
private void send(final Position position) {
|
||||||
log("send", position);
|
log("send", position);
|
||||||
lock();
|
|
||||||
String request = ProtocolFormatter.formatRequest(url, position);
|
String request = ProtocolFormatter.formatRequest(url, position);
|
||||||
RequestManager.sendRequestAsync(request, new RequestManager.RequestHandler() {
|
RequestManager.sendRequestAsync(request, new RequestManager.RequestHandler() {
|
||||||
@Override
|
@Override
|
||||||
@@ -201,7 +178,6 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
StatusActivity.addMessage(context.getString(R.string.status_send_fail));
|
StatusActivity.addMessage(context.getString(R.string.status_send_fail));
|
||||||
retry();
|
retry();
|
||||||
}
|
}
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org)
|
* Copyright 2012 - 2019 Anton Tananaev (anton@traccar.org)
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.traccar.client;
|
package org.traccar.client;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
@@ -26,6 +27,8 @@ import android.os.Build;
|
|||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class TrackingService extends Service {
|
public class TrackingService extends Service {
|
||||||
@@ -33,6 +36,7 @@ public class TrackingService extends Service {
|
|||||||
private static final String TAG = TrackingService.class.getSimpleName();
|
private static final String TAG = TrackingService.class.getSimpleName();
|
||||||
private static final int NOTIFICATION_ID = 1;
|
private static final int NOTIFICATION_ID = 1;
|
||||||
|
|
||||||
|
private PowerManager.WakeLock wakeLock;
|
||||||
private TrackingController trackingController;
|
private TrackingController trackingController;
|
||||||
|
|
||||||
private static Notification createNotification(Context context) {
|
private static Notification createNotification(Context context) {
|
||||||
@@ -73,6 +77,7 @@ public class TrackingService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("WakelockTimeout")
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
Log.i(TAG, "service create");
|
Log.i(TAG, "service create");
|
||||||
@@ -81,6 +86,11 @@ public class TrackingService extends Service {
|
|||||||
startForeground(NOTIFICATION_ID, createNotification(this));
|
startForeground(NOTIFICATION_ID, createNotification(this));
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
|
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||||
|
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
||||||
|
wakeLock.acquire();
|
||||||
|
|
||||||
trackingController = new TrackingController(this);
|
trackingController = new TrackingController(this);
|
||||||
trackingController.start();
|
trackingController.start();
|
||||||
}
|
}
|
||||||
@@ -111,6 +121,9 @@ public class TrackingService extends Service {
|
|||||||
|
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
|
|
||||||
|
if (wakeLock != null && wakeLock.isHeld()) {
|
||||||
|
wakeLock.release();
|
||||||
|
}
|
||||||
if (trackingController != null) {
|
if (trackingController != null) {
|
||||||
trackingController.stop();
|
trackingController.stop();
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user