2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 14:15:14 +00:00

Use registerNetworkCallback to listen to network changes

ConnectivityManager.CONNECTIVITY_ACTION was deprecated in Android 9
This commit is contained in:
Albert Vaca Cintora
2022-12-31 00:35:08 +01:00
parent 4be79b52ae
commit fd34e56675

View File

@@ -17,6 +17,8 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
@@ -262,11 +264,27 @@ public class BackgroundService extends Service {
// Register screen on listener
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
// See: https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
}
registerReceiver(new KdeConnectBroadcastReceiver(), filter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
cm.registerNetworkCallback(builder.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
onDeviceListChanged();
onNetworkChange();
}
@Override
public void onLost(Network network) {
onDeviceListChanged();
}
});
}
Log.i("KDE/BackgroundService", "Service not started yet, initializing...");
PluginFactory.initPluginInfo(getBaseContext());