Android Detect Internet Connection Status
When we use internet connection for our operation,first we need to check internet connection availability.For example : Invoke web services,network operation,etc.
You can download source code here.
This is code for GenerateCodeActivity. It will invoke SOAP response from our server.
You can download source code here.
This is code for GenerateCodeActivity. It will invoke SOAP response from our server.
package com.etr.securitycode;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class GenerateCodeActivity extends Activity {
private String METHOD_NAME = "CodeGenerate";
private String NAMESPACE = "http://tempuri.org/";
private String SOAP_ACTION = "http://tempuri.org/IService1/CodeGenerate";
private static final String URL = "http://easprojects.sifytest.net/MyServices/Service1.svc?wsdl";
Object result;
EditText username, password, code;
Button generate;
NetWorkStatus status;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate_code);
username = (EditText) findViewById(R.id.editText1);
password = (EditText) findViewById(R.id.editText2);
code = (EditText) findViewById(R.id.editText3);
generate = (Button) findViewById(R.id.button1);
status = new NetWorkStatus(this);
generate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (status.isConnecting()) {
showAlertDialog(GenerateCodeActivity.this,
"Internet connection",
"You have internet connection");
new Invoke().execute();
} else {
showAlertDialog(GenerateCodeActivity.this,
"No Internet connection",
"You don't have internet connection");
}
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.generate_code, menu);
return true;
}
public class Invoke extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(GenerateCodeActivity.this);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userName", username.getText().toString());
request.addProperty("password", password.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
} catch (Exception e1) {
Log.v("JSONCall Exception", Log.getStackTraceString(e1));
}
return null;
}
protected void onPostExecute(Void name) {
username.setText("");
password.setText("");
code.setText(result.toString());
dialog.dismiss();
}
}
public void showAlertDialog(Context context, String title, String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(
GenerateCodeActivity.this);
builder.setTitle(title).setMessage(msg).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class GenerateCodeActivity extends Activity {
private String METHOD_NAME = "CodeGenerate";
private String NAMESPACE = "http://tempuri.org/";
private String SOAP_ACTION = "http://tempuri.org/IService1/CodeGenerate";
private static final String URL = "http://easprojects.sifytest.net/MyServices/Service1.svc?wsdl";
Object result;
EditText username, password, code;
Button generate;
NetWorkStatus status;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate_code);
username = (EditText) findViewById(R.id.editText1);
password = (EditText) findViewById(R.id.editText2);
code = (EditText) findViewById(R.id.editText3);
generate = (Button) findViewById(R.id.button1);
status = new NetWorkStatus(this);
generate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (status.isConnecting()) {
showAlertDialog(GenerateCodeActivity.this,
"Internet connection",
"You have internet connection");
new Invoke().execute();
} else {
showAlertDialog(GenerateCodeActivity.this,
"No Internet connection",
"You don't have internet connection");
}
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.generate_code, menu);
return true;
}
public class Invoke extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(GenerateCodeActivity.this);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userName", username.getText().toString());
request.addProperty("password", password.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
} catch (Exception e1) {
Log.v("JSONCall Exception", Log.getStackTraceString(e1));
}
return null;
}
protected void onPostExecute(Void name) {
username.setText("");
password.setText("");
code.setText(result.toString());
dialog.dismiss();
}
}
public void showAlertDialog(Context context, String title, String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(
GenerateCodeActivity.this);
builder.setTitle(title).setMessage(msg).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
This is code for NetWorkStatus. It will check network availability using ConnectivityManager.
We need permission for checking internet availability.
Output :
package com.etr.securitycode;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetWorkStatus {
private Context _context;
public NetWorkStatus(Context context) {
this._context = context;
}
public boolean isConnecting() {
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;
}
}
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetWorkStatus {
private Context _context;
public NetWorkStatus(Context context) {
this._context = context;
}
public boolean isConnecting() {
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;
}
}
We need permission for checking internet availability.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Output :
Comments
Post a Comment