Java Code Examples for android.net.ConnectivityManager#unregisterNetworkCallback()

The following examples show how to use android.net.ConnectivityManager#unregisterNetworkCallback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScreenOnOffUpdateService.java    From your-local-weather with GNU General Public License v3.0 6 votes vote down vote up
public void stopSensorBasedUpdates() {
    appendLog(getBaseContext(), TAG, "STOP_SENSOR_BASED_UPDATES recieved");
    try {
        getApplication().unregisterReceiver(screenOnReceiver);
        getApplication().unregisterReceiver(screenOffReceiver);
        getApplication().unregisterReceiver(userUnlockedReceiver);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            getApplicationContext().unregisterReceiver(networkConnectivityReceiver);
        } else {
            ConnectivityManager connectivityManager
                    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            connectivityManager.unregisterNetworkCallback(networkConnectionReceiver);
        }
    } catch (Exception e) {
        appendLog(getBaseContext(), TAG, "Error unregistering screen receivers - receivers was not registered");
    }
}
 
Example 2
Source File: RequirementsWatcher.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void unregisterNetworkCallback() {
  if (Util.SDK_INT >= 21) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.unregisterNetworkCallback(networkCallback);
    networkCallback = null;
  }
}
 
Example 3
Source File: RequirementsWatcher.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void unregisterNetworkCallback() {
  if (Util.SDK_INT >= 21) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.unregisterNetworkCallback(networkCallback);
    networkCallback = null;
  }
}
 
Example 4
Source File: FadeInNetworkImageView.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(21)
@Override
public void setImageUrl(String url, ImageLoader imageLoader) {
    mShowLocal = TextUtils.isEmpty(url);
    if (mDiskCache.get(url) == null && !OnlineAccessVerifier.check(getContext())) {
        this.setImageBitmap(null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            final ConnectivityManager cm = (ConnectivityManager)
                    getContext().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            networkCallback = new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    super.onAvailable(network);
                    new CoverArtLoader((MainActivity) FadeInNetworkImageView.this.getActivity()).execute(mLyrics);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && networkCallback != null)
                        try {
                            cm.unregisterNetworkCallback(networkCallback);
                        } catch (IllegalArgumentException ignored) {
                        }
                }
            };
            NetworkRequest request = new NetworkRequest.Builder()
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
            cm.registerNetworkCallback(request, networkCallback);
        }
        defaultShown = true;
    } else {
        super.setImageUrl(url, imageLoader);
        if (!TextUtils.isEmpty(url))
            defaultShown = false;
    }
}
 
Example 5
Source File: RequirementsWatcher.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private void unregisterNetworkCallback() {
  if (Util.SDK_INT >= 21) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.unregisterNetworkCallback(networkCallback);
    networkCallback = null;
  }
}
 
Example 6
Source File: PhoneUtils.java    From Mobilyzer with Apache License 2.0 5 votes vote down vote up
public void unregisterNetworkCallback(){
	if (connectivityNetworkCallback!=null){
		ConnectivityManager cm=((ConnectivityNetworkCallback)connectivityNetworkCallback).getConnectivityManager();
		cm.bindProcessToNetwork(null);
		cm.unregisterNetworkCallback(connectivityNetworkCallback);
		connectivityNetworkCallback = null;
	}
}
 
Example 7
Source File: HomeActivity.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPause() {
    dribbblePrefs.removeLoginStatusListener(filtersAdapter);
    if (monitoringConnectivity) {
        final ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        connectivityManager.unregisterNetworkCallback(connectivityCallback);
        monitoringConnectivity = false;
    }
    super.onPause();
}
 
Example 8
Source File: ConnectivityNetworkCallback.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
public static void unregister() {
	if(_impl != null) {
		ConnectivityManager cm = (ConnectivityManager)ClientProperties.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		cm.unregisterNetworkCallback(_impl);

		_impl = null;
	}
}
 
Example 9
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.Q)
private static boolean disconnectAndroidQ(@NonNull final ConnectivityManager connectivityManager) {
    if (networkCallback != null) {
        connectivityManager.unregisterNetworkCallback(networkCallback);
        networkCallback = null;
    }

    return true;
}
 
Example 10
Source File: RequirementsWatcher.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private void unregisterNetworkCallback() {
  if (Util.SDK_INT >= 21) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.unregisterNetworkCallback(Assertions.checkNotNull(networkCallback));
    networkCallback = null;
  }
}
 
Example 11
Source File: FragmentOptionsConnection.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPause() {
    ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return;

    cm.unregisterNetworkCallback(networkCallback);

    super.onPause();
}
 
Example 12
Source File: ServiceVPN.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void unlistenNetworkChanges() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        cm.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkCallback);
    }
}
 
Example 13
Source File: ModulesBroadcastReceiver.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void unlistenNetworkChanges() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        cm.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkCallback);
    }
}
 
Example 14
Source File: RequirementsWatcher.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void unregisterNetworkCallback() {
  if (Util.SDK_INT >= 21) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.unregisterNetworkCallback(networkCallback);
    networkCallback = null;
  }
}
 
Example 15
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 4 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.Q)
private static boolean connectAndroidQ(@Nullable ConnectivityManager connectivityManager, @NonNull ScanResult scanResult, @NonNull String password) {
    if (connectivityManager == null) {
        return false;
    }

    WifiNetworkSpecifier.Builder wifiNetworkSpecifierBuilder = new WifiNetworkSpecifier.Builder()
            .setSsid(scanResult.SSID)
            .setBssid(MacAddress.fromString(scanResult.BSSID));

    final String security = ConfigSecurities.getSecurity(scanResult);

    ConfigSecurities.setupWifiNetworkSpecifierSecurities(wifiNetworkSpecifierBuilder, security, password);


    NetworkRequest networkRequest = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .setNetworkSpecifier(wifiNetworkSpecifierBuilder.build())
            .build();

    // not sure, if this is needed
    if (networkCallback != null) {
        connectivityManager.unregisterNetworkCallback(networkCallback);
    }

    networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(@NonNull Network network) {
            super.onAvailable(network);

            wifiLog("AndroidQ+ connected to wifi ");

            // bind so all api calls are performed over this new network
            connectivityManager.bindProcessToNetwork(network);
        }

        @Override
        public void onUnavailable() {
            super.onUnavailable();

            wifiLog("AndroidQ+ could not connect to wifi");
        }
    };

    connectivityManager.requestNetwork(networkRequest, networkCallback);

    return true;
}
 
Example 16
Source File: ServiceSend.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onDestroy() {
    EntityLog.log(this, "Service send destroy");

    unregisterReceiver(connectionChangedReceiver);

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.unregisterNetworkCallback(networkCallback);

    owner.stop();
    handling.clear();

    stopForeground(true);

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(Helper.NOTIFICATION_SEND);

    super.onDestroy();
}
 
Example 17
Source File: ServiceSynchronize.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void onDestroy() {
        EntityLog.log(this, "Service destroy");

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.unregisterOnSharedPreferenceChangeListener(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            unregisterReceiver(idleModeChangedReceiver);

        unregisterReceiver(connectionChangedReceiver);

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.unregisterNetworkCallback(networkCallback);

        liveAccountNetworkState.postDestroy();

        try {
            stopForeground(true);
        } catch (Throwable ex) {
            Log.e(ex);
/*
            OnePlus A6013 - Android 9

            java.lang.RuntimeException: Unable to stop service eu.faircode.email.ServiceSynchronize@3995fc9: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
                    at android.app.ActivityThread.handleStopService(ActivityThread.java:3908)
                    at android.app.ActivityThread.access$1900(ActivityThread.java:209)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1826)
                    at android.os.Handler.dispatchMessage(Handler.java:106)
                    at android.os.Looper.loop(Looper.java:193)
                    at android.app.ActivityThread.main(ActivityThread.java:6954)
                    at java.lang.reflect.Method.invoke(Method.java:-2)
                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
                    at android.os.Parcel.createException(Parcel.java:1956)
                    at android.os.Parcel.readException(Parcel.java:1918)
                    at android.os.Parcel.readException(Parcel.java:1868)
                    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5111)
                    at android.app.Service.stopForeground(Service.java:724)
                    at android.app.Service.stopForeground(Service.java:710)
*/
        }

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(Helper.NOTIFICATION_SYNCHRONIZE);

        super.onDestroy();
    }
 
Example 18
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void unlistenNetworkChanges() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkCallback);
}
 
Example 19
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void unlistenNetworkChanges() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkCallback);
}