-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkAvailability.java
95 lines (80 loc) · 3.77 KB
/
NetworkAvailability.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.foo.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.support.annotation.NonNull;
public class NetworkAvailability {
/* TODO: CHANGE THE ACTION NAME TO BE RELEVANT TO YOUR PROJECT */
public static final String NETWORK_AVAILABILITY_ACTION = "com.foo.NETWORK_AVAILABILITY_ACTION";
private ConnectivityManager connectivityManager;
private ConnectivityManager.NetworkCallback networkCallback;
private static NetworkAvailability instance;
private NetworkAvailability() {
}
public static NetworkAvailability getInstance(){
if(instance == null){
instance = new NetworkAvailability();
}
return instance;
}
public static boolean isAvailable(Context context) {
ConnectivityManager cm =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
public void registerNetworkAvailability(final Context context, BroadcastReceiver networkAvailabilityReceiver) {
context.registerReceiver(networkAvailabilityReceiver, new IntentFilter(NETWORK_AVAILABILITY_ACTION));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
context.registerReceiver(connectivityChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
} else{
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
context.sendBroadcast(getNetworkAvailabilityIntent(true));
}
@Override
public void onLost(Network network) {
context.sendBroadcast(getNetworkAvailabilityIntent(false));
}
};
connectivityManager.registerNetworkCallback(builder.build(), networkCallback);
if(isAvailable(context)){
context.sendBroadcast(getNetworkAvailabilityIntent(true));
} else{
context.sendBroadcast(getNetworkAvailabilityIntent(false));
}
}
}
public void unregisterNetworkAvailability(Context context, BroadcastReceiver networkAvailabilityReceiver){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
context.unregisterReceiver(connectivityChangeReceiver);
} else{
connectivityManager.unregisterNetworkCallback(networkCallback);
}
context.unregisterReceiver(networkAvailabilityReceiver);
}
private BroadcastReceiver connectivityChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
context.sendBroadcast(getNetworkAvailabilityIntent(false));
} else {
context.sendBroadcast(getNetworkAvailabilityIntent(true));
}
}
};
@NonNull
private Intent getNetworkAvailabilityIntent(boolean isNetworkAvailable) {
Intent intent = new Intent(NETWORK_AVAILABILITY_ACTION);
intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, !isNetworkAvailable);
return intent;
}
}