mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 07:02:51 +08:00
Network Reachability
This commit is contained in:
parent
259d441b61
commit
8d80ebac11
@ -47,6 +47,7 @@ public class DroidGap extends Activity {
|
||||
private AccelListener accel;
|
||||
private CameraLauncher launcher;
|
||||
private FileUtils fs;
|
||||
private NetworkManager netMan;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@ -100,6 +101,7 @@ public class DroidGap extends Activity {
|
||||
accel = new AccelListener(this, appView);
|
||||
launcher = new CameraLauncher(appView, this);
|
||||
fs = new FileUtils(appView);
|
||||
netMan = new NetworkManager(this, appView);
|
||||
|
||||
// This creates the new javascript interfaces for PhoneGap
|
||||
appView.addJavascriptInterface(gap, "DroidGap");
|
||||
@ -107,6 +109,7 @@ public class DroidGap extends Activity {
|
||||
appView.addJavascriptInterface(accel, "Accel");
|
||||
appView.addJavascriptInterface(launcher, "GapCam");
|
||||
appView.addJavascriptInterface(fs, "FileUtil");
|
||||
appView.addJavascriptInterface(netMan, "NetworkManager");
|
||||
}
|
||||
|
||||
/**
|
||||
|
41
src/com/phonegap/demo/NetworkManager.java
Normal file
41
src/com/phonegap/demo/NetworkManager.java
Normal file
@ -0,0 +1,41 @@
|
||||
package com.phonegap.demo;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.*;
|
||||
import android.webkit.WebView;
|
||||
|
||||
public class NetworkManager {
|
||||
|
||||
Context mCtx;
|
||||
WebView mView;
|
||||
ConnectivityManager sockMan;
|
||||
|
||||
NetworkManager(Context ctx, WebView view)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mView = view;
|
||||
sockMan = (ConnectivityManager) mCtx.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
}
|
||||
|
||||
public boolean isAvailable()
|
||||
{
|
||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||
return info.isConnected();
|
||||
}
|
||||
|
||||
public boolean isReachable(String uri)
|
||||
{
|
||||
boolean reached = true;
|
||||
try {
|
||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
HttpGet httpget = new HttpGet(uri);
|
||||
httpclient.execute(httpget);
|
||||
} catch (Exception e) { reached = false;}
|
||||
return reached;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user