This repository was archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkUtil.java
More file actions
52 lines (40 loc) · 1.54 KB
/
NetworkUtil.java
File metadata and controls
52 lines (40 loc) · 1.54 KB
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
package ngo.sapne.intents.sapne;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
/**
* Created by yogi on 24-11-2017.
*/
public class NetworkUtil {
static Context context;
/**
* We use this class to determine if the application has been connected to either WIFI Or Mobile
* Network, before we make any network request to the server.
* <p>
* The class uses two permission - INTERNET and ACCESS NETWORK STATE, to determine the user's
* connection stats
*/
private static NetworkUtil instance = new NetworkUtil();
ConnectivityManager connectivityManager;
boolean connected = false;
public static NetworkUtil getInstance(Context ctx) {
context = ctx.getApplicationContext();
return instance;
}
public boolean isOnline() {
try {
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
return connected;
} catch (Exception e) {
System.out.println("CheckConnectivity Exception: " + e.getMessage());
Log.v("connectivity", e.toString());
}
return connected;
}
}