问题
In my application i want to send current location and time continuously each 5min In case network is not available app has to store data (here im using sqlite to store) and send data once network enabled or internet connected.
i goggled lot but i could see only can check mobile or wifi network enabled or not. in some cases wifi or mobile network may be enabled but internet will not be work. In this case how can i find internet is working or not?
I hope you got my problem
回答1:
The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none if the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
You will also need:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in your android manifest.
Note that having an active network interface doesn't guarantee that a particular networked service is available. Networks issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server. For instance you can't tell for sure if your app can reach Twitter until you receive a valid response from the Twitter service.
回答2:
Try with this code it may help you :
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkUtils {
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connection = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = null;
if (connection != null) {
nInfo = connection.getActiveNetworkInfo();
}
if (nInfo == null || !nInfo.isConnectedOrConnecting()) {
return false;
}
if (nInfo == null || !nInfo.isConnected()) {
return false;
}
if (nInfo != null
&& ((nInfo.getType() == ConnectivityManager.TYPE_MOBILE) || (nInfo
.getType() == ConnectivityManager.TYPE_WIFI))) {
if (nInfo.getState() != NetworkInfo.State.CONNECTED
|| nInfo.getState() == NetworkInfo.State.CONNECTING) {
return false;
}
}
return true;
}
}
Use this method in your base class and call it where ever you want to check network .
public boolean isNetworkAvailable() {
if (NetworkUtils.isNetworkAvailable(getActivity())) {
return true;
} else {
BaseDialogue.showCustomDialogue(getActivity(), getResources()
.getString(R.string.app_name),
"No Network",
getResources().getString(android.R.string.ok),
getResources().getString(android.R.string.cancel), null);
return false;
}
}
回答3:
This is what I am using:
private boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)v.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) return true;
return false;
}
This permission is needed:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
回答4:
public class InternetData {
Activity activity;
ConnectivityManager connManager;
NetworkInfo mWifi;
public InternetData(Activity activity){
this.activity = activity;
connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
}
public boolean hasInternetConnection(){
if(mWifi.isConnected())
return true;
return false;
}
}
回答5:
You can go this way too:
Create two java files in your package:NetworkConnectivity.java
and NetworkMonitorListener.java
as below:
NetworkConnectivity.java:
public class NetworkConnectivity {
private static NetworkConnectivity sharedNetworkConnectivity = null;
private Activity activity = null;
private final Handler handler = new Handler();
private Runnable runnable = null;
private boolean stopRequested = false;
private boolean monitorStarted = false;
private static final int NETWORK_CONNECTION_YES = 1;
private static final int NETWORK_CONNECTION_NO = -1;
private static final int NETWORK_CONNECTION_UKNOWN = 0;
private int connected = NETWORK_CONNECTION_UKNOWN;
public static final int MONITOR_RATE_WHEN_CONNECTED_MS = 5000;
public static final int MONITOR_RATE_WHEN_DISCONNECTED_MS = 1000;
private final List<NetworkMonitorListener> networkMonitorListeners = new ArrayList<NetworkMonitorListener>();
private NetworkConnectivity() {
}
public synchronized static NetworkConnectivity sharedNetworkConnectivity() {
if (sharedNetworkConnectivity == null) {
sharedNetworkConnectivity = new NetworkConnectivity();
}
return sharedNetworkConnectivity;
}
public void configure(Activity activity) {
this.activity = activity;
}
public synchronized boolean startNetworkMonitor() {
if (this.activity == null) {
return false;
}
if (monitorStarted) {
return true;
}
stopRequested = false;
monitorStarted = true;
(new Thread(new Runnable() {
@Override
public void run() {
doCheckConnection();
}
})).start();
return true;
}
public synchronized void stopNetworkMonitor() {
stopRequested = true;
monitorStarted = false;
}
public void addNetworkMonitorListener(NetworkMonitorListener l) {
this.networkMonitorListeners.add(l);
this.notifyNetworkMonitorListener(l);
}
public boolean removeNetworkMonitorListener(NetworkMonitorListener l) {
return this.networkMonitorListeners.remove(l);
}
private void doCheckConnection() {
if (stopRequested) {
runnable = null;
return;
}
final boolean connectedBool = this.isConnected();
final int _connected = (connectedBool ? NETWORK_CONNECTION_YES
: NETWORK_CONNECTION_NO);
if (this.connected != _connected) {
this.connected = _connected;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
notifyNetworkMonitorListeners();
}
});
}
runnable = new Runnable() {
@Override
public void run() {
doCheckConnection();
}
};
handler.postDelayed(runnable,
(connectedBool ? MONITOR_RATE_WHEN_CONNECTED_MS
: MONITOR_RATE_WHEN_DISCONNECTED_MS));
}
public boolean isConnected() {
try {
ConnectivityManager cm = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
private void notifyNetworkMonitorListener(NetworkMonitorListener l) {
try {
if (this.connected == NETWORK_CONNECTION_YES) {
l.connectionEstablished();
} else if (this.connected == NETWORK_CONNECTION_NO) {
l.connectionLost();
} else {
l.connectionCheckInProgress();
}
} catch (Exception e) {
}
}
private void notifyNetworkMonitorListeners() {
for (NetworkMonitorListener l : this.networkMonitorListeners) {
this.notifyNetworkMonitorListener(l);
}
}
}
NetworkMonitorListener.java:
public interface NetworkMonitorListener {
public void connectionEstablished();
public void connectionLost();
public void connectionCheckInProgress();
}
And at last, use this class in your activity as below, say for example:
NetworkConnectivity.sharedNetworkConnectivity().configure(this);
NetworkConnectivity.sharedNetworkConnectivity().startNetworkMonitor();
NetworkConnectivity.sharedNetworkConnectivity()
.addNetworkMonitorListener(new NetworkMonitorListener() {
@Override
public void connectionCheckInProgress() {
// Okay to make UI updates (check-in-progress is rare)
}
@Override
public void connectionEstablished() {
// Okay to make UI updates -- do something now that
// connection is avaialble
}
@Override
public void connectionLost() {
// Okay to make UI updates -- bummer, no connection
}
});
With this way, you can check internet connection in real time, and act accordingly.
回答6:
Try to implement this method in your project:
private boolean netCheckin() {
try {
ConnectivityManager nInfo = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
Log.d(tag, "Net avail:"
+ nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
ConnectivityManager cm = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Log.d(tag, "Network Available");
return true;
} else {
Log.d(tag, "Network Not Available");
return false;
}
} catch (Exception e) {
return false;
}
}
and also check internet permission is given or not in AndroidManifest file.
来源:https://stackoverflow.com/questions/20704427/how-to-check-internet-is-working-or-not-not-meant-disabled-or-not-in-android