I need code for this Algorithm in android Eclipse programming :
If internet connection = connect then open program
else show error_activity.xml
I need code for this Algorithm in android Eclipse programming :
If internet connection = connect then open program
else show error_activity.xml
Create a class ConnectionDetector and put the below code in that class:
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
// now where ever you want to check internet just make a object of this class and call its method like below i am doing:
// in your activity define globals:
Boolean _isInternetPresent = false;
ConnectionDetector _cd;
//in oncreate
_cd = new ConnectionDetector(getApplicationContext());
_isInternetPresent = _cd.isConnectingToInternet();
and check like this:
if (_isInternetPresent) {
// do you work here
} else {
// no internet,please try again.
}
as you are a fresher i am giving you complete code.please try to understand each and every step,how this code is working.
try this to check for internet connection..
public void checkNetwork() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
//true : your code
} else {
//false : your code
Log.v("Network Error", "No network connection available.");
}
}
Step 1. Create ConnectionDetector.java
package com.YOURPACKAGE.YOURAPP;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean checkInternetConn() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(info == null){
connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}
if(info == null){
connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
}
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
return false;
}
}
Step 2. Implement the Connection Detector in your Activity
public class YOURACTIVITY extends Activity {
ConnectionDetector cd;
private void methodThatNeedsTheInternet() {
Boolean isConnectionExist = false;
isConnectionExist = cd.checkInternetConn();
// check for Internet status before proceeding
if (!isConnectionExist) {
//no internet, show alert or something
return;
}
//internet works, perform method that needs it
}
}