我有一个前台服务,它有一个运行网络循环监听udp数据包的对象。在设备屏幕锁定之前,它工作正常。那么它就无法接收任何数据包。我需要在包裹到达时出示通知。下面是我如何创建服务的。
public class BackgroundService extends LifecycleService {
private static final String TAG = "BackgroundService";
public static final int FOREGROUND_NOTIFICATION_ID = 255;
public static final String AUTH_CANCEL_ACTION = "com.anjanik012.suto.AUTH_CANCEL";
private SutoConnection connection;
private AuthenticationCancelReceiver authenticationCancelReceiver;
@Override
public void onCreate() {
super.onCreate();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(FOREGROUND_NOTIFICATION_ID, createNotification());
}
connection = SutoConnection.getInstance(getApplication());
connection.start();
authenticationCancelReceiver = new AuthenticationCancelReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(AUTH_CANCEL_ACTION);
registerReceiver(authenticationCancelReceiver, filter);
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(@NonNull Intent intent) {
super.onBind(intent);
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
connection.stop();
unregisterReceiver(authenticationCancelReceiver);
stopSelf();
}
@RequiresApi(api = Build.VERSION_CODES.O)
Notification createNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
NotificationChannelsManager.foregroundChannelId);
builder.setSmallIcon(R.drawable.ic_notif_white)
.setContentIntent(pendingIntent)
.setContentText(getText(R.string.foreground_notification_msg))
.setPriority(Notification.PRIORITY_LOW)
.setCategory(Notification.CATEGORY_SERVICE);
return builder.build();
}
这是网络环路 SutoConnection.start()
```
public void start() {
listening = true;
DatagramPacket packet = new DatagramPacket(buff, BUFF_LENGTH);
Thread daemon = new Thread(() -> {
while (listening) {
if (currentState != States.TCP_CONNECTED) {
currentState = States.ACTIVE;
Log.d(TAG, "run: Start UDP listening thread");
Log.d(TAG, "run: Thread ID:- " + Thread.currentThread().getId());
try {
udpSocket.receive(packet);
String msg = StringUtils.substringBefore(new String(packet.getData(), StandardCharsets.UTF_8), "+");
Log.d(TAG, "run: UDP Message received:-" + msg);
Host host = validateHelloMsg(msg);
if (host != null) {
// Host entry is in database;
// Open TCP connection to the client
SocketFactory socketFactory = SocketFactory.getDefault();
try {
clientAddr = (Inet4Address) packet.getAddress();
tcpSocket = socketFactory.createSocket(clientAddr, TCP_PORT);
} catch (ConnectException connectException) {
Log.e(TAG, "start: suto client offline, this packet is probably an old packet...ignoring", connectException);
continue;
}
if (tcpSocket.isConnected()) {
currentState = States.TCP_CONNECTED;
Log.d(TAG, "run: TCP Connection established");
inputStream = tcpSocket.getInputStream();
outputStream = tcpSocket.getOutputStream();
protocol.init(host);
}
}
} catch (IOException e) {
Log.e(TAG, "run: Error reading data from suto client", e);
}
}
}
});
daemon.setDaemon(true);
daemon.start();
}
这根线止于 `udpSocket.receive(packet);` 当屏幕锁定时。当设备解锁时,它开始接收数据包。
当设备被锁定时,如何继续接收数据包?
暂无答案!
目前还没有任何答案,快来回答吧!