Check connected before send

pull/152/head
M66B 7 years ago
parent d32b55d2c5
commit 7257d74a5e

@ -138,8 +138,27 @@ public class ServiceSend extends LifecycleService {
@Override
public void onAvailable(Network network) {
Log.i("Service send available=" + network);
if (isConnected())
run();
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
Log.i("Service send caps=" + caps);
if (isConnected())
run();
}
private boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return (ni != null && ni.isConnected());
}
private void run() {
if (thread != null && thread.isAlive())
return;
if (thread == null || !thread.isAlive()) {
thread = new Thread(new Runnable() {
@Override
public void run() {
@ -155,8 +174,6 @@ public class ServiceSend extends LifecycleService {
db.folder().setFolderError(outbox.id, null);
db.folder().setFolderSyncState(outbox.id, "syncing");
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
List<EntityOperation> ops = db.operation().getOperations(outbox.id);
Log.i(outbox.name + " pending operations=" + ops.size());
for (EntityOperation op : ops) {
@ -200,8 +217,7 @@ public class ServiceSend extends LifecycleService {
Log.i(outbox.name + " end op=" + op.id + "/" + op.name);
}
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null || !ni.isConnected())
if (!isConnected())
break;
}
@ -221,7 +237,6 @@ public class ServiceSend extends LifecycleService {
});
thread.start();
}
}
};
private void send(EntityMessage message) throws MessagingException, IOException {

Loading…
Cancel
Save