Ensure main thread

This commit is contained in:
M66B 2019-12-09 12:00:41 +01:00
parent 3447bf0236
commit 195b40a330
1 changed files with 20 additions and 4 deletions

View File

@ -34,6 +34,7 @@ import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
@ -135,7 +136,26 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
post(false, lastNetworkState, lastAccountStates);
}
private void postDestroy() {
postValue(null);
}
private void post(boolean reload, ConnectionHelper.NetworkState networkState, List<TupleAccountState> accountStates) {
if (Looper.myLooper() == Looper.getMainLooper())
_post(reload, networkState, accountStates);
else {
// Some Android versions call onDestroy not on the main thread
Log.e("### not main thread state=" + (accountStates == null ? null : accountStates.size()));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
_post(reload, networkState, accountStates);
}
});
}
}
private void _post(boolean reload, ConnectionHelper.NetworkState networkState, List<TupleAccountState> accountStates) {
if (networkState != null && accountStates != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
boolean enabled = prefs.getBoolean("enabled", true);
@ -155,10 +175,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
postValue(result);
}
}
private void postDestroy() {
postValue(null);
}
}
@Override