试试下面的代码,我希望它能解决你的问题。
供参考:
https://github.com/oscarjiv91/Android-Check-Internet-Connection
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;
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(){
HttpGet httpGet = new HttpGet(url_ping);
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 7000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
try{
httpClient.execute(httpGet);
mConnectionServiceCallback.hasInternetConnection();
return true;
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
mConnectionServiceCallback.hasNoInternetConnection();
return false;
}
}
在您的活动中,您要在哪里启动此服务:
Intent intent = new Intent(this, ConnectionService.class);
// Interval in seconds
intent.putExtra(ConnectionService.TAG_INTERVAL, 100);
// URL to ping
intent.putExtra(ConnectionService.TAG_URL_PING, "http://www.google.com");
// Name of the class that is calling this service
intent.putExtra(ConnectionService.TAG_ACTIVITY_NAME, this.getClass().getName());
// Starts the service
startService(intent);
实现连接服务。ConnectionService回调活动并重写方法:
@Override
public void hasInternetConnection() {
// has internet
}
@Override
public void hasNoInternetConnection() {
// no internet :(
}
停止服务
stopService(new Intent(this, ConnectionService.class));