forked from oscarjiv91/Android-Check-Internet-Connection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionService.java
More file actions
102 lines (81 loc) · 2.83 KB
/
Copy pathConnectionService.java
File metadata and controls
102 lines (81 loc) · 2.83 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
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
96
97
98
99
100
101
package services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Oscar on 20/03/15.
*/
public class ConnectionService extends Service {
// Constant
public static String TAG_INTERVAL = "interval";
public static String TAG_URL_PING = "url_ping";
public static String TAG_ACTIVITY_NAME = "activity_name";
private int interval;
private String url_ping;
private String activity_name;
private Timer mTimer = null;
ConnectionServiceCallback mConnectionServiceCallback;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public interface ConnectionServiceCallback {
void hasInternetConnection();
void hasNoInternetConnection();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
interval = intent.getIntExtra(TAG_INTERVAL, 10);
url_ping = intent.getStringExtra(TAG_URL_PING);
activity_name = intent.getStringExtra(TAG_ACTIVITY_NAME);
try {
mConnectionServiceCallback = (ConnectionServiceCallback) Class.forName(activity_name).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new CheckForConnection(), 0, interval * 1000);
return super.onStartCommand(intent, flags, startId);
}
class CheckForConnection extends TimerTask{
@Override
public void run() {
isNetworkAvailable();
}
}
@Override
public void onDestroy() {
mTimer.cancel();
super.onDestroy();
}
private boolean isNetworkAvailable(){
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL(url_ping).openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
if ((urlc.getResponseCode() == 200)) {
mConnectionServiceCallback.hasInternetConnection();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
mConnectionServiceCallback.hasNoInternetConnection();
return false;
}
}