How to Check Internet Connection in Android
Hi!
Some of new android programmers facing the one of the problem to check the internet connectivity.
I have write the blow class for to check the internet connection.
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class RCheckInternet {
boolean is_Rconnected=true;
private int TYPE_WIFI=1,TYPE_MOBILE = 2,TYPE_NOT_CONNECTED = 0;
private int isInternetAvailabl(Context context) {
ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf=cm.getActiveNetworkInfo();
if (null != nf) {
if(nf.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;
if(nf.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}
public boolean is_Rconnected(Context context) {
int conn = isInternetAvailabl(context);
if (conn == TYPE_NOT_CONNECTED)
{
is_Rconnected=false;
}
return is_Rconnected;
}
}
it is enough to pass the application context & you will receive the status of the current connection
public class Main extends Activity implements OnClickListener{
Button lin,regs;
Login_Emenu_Manager lgMan;
EditText un,pw;
boolean log_chk = false;
String url_lin="http:my link to pass the variables";//?Email,Password
public ProgressDialog pDialog;
// create reference for the class
RCheckInternet r_nw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
lgMan = new Login_Emenu_Manager(getApplicationContext());
// create an object for the class
r_nw=new RCheckInternet();
lin.setOnClickListener(this);
regs.setOnClickListener(this);
}
public void init()
{
lin=(Button) findViewById(R.id.bn_login);
regs=(Button) findViewById(R.id.bnreg);
un=(EditText) findViewById(R.id.r_email);
pw=(EditText) findViewById(R.id.r_pass);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bn_login:
if(!un.getText().toString().equals(""))
{
if(!pw.getText().toString().equals(""))
{
//pass the context of the current class to check the internet status
if(r_nw.is_Rconnected(getApplicationContext()))
{
new MainAsy().execute();
}
else
{
Toast.makeText(getApplicationContext(), "Not connected to Internet", Toast.LENGTH_SHORT).show();
}
}
else
{
pw.setError(Html.fromHtml("<font color='green'>Enter Valid Password</font>"));
}
}
else
{
un.setError(Html.fromHtml("<font color='green'>Enter Valid UserName</font>"));
}
break;
case R.id.bnreg:
Intent ii=new Intent(this,Register.class);
startActivity(ii);
break;
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
super.onBackPressed();
}
class MainAsy extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(Main.this);
pDialog.setMessage("LOGIN..,");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
List<NameValuePair> sv_param = new ArrayList<NameValuePair>();
JSONParser sv_jparse = new JSONParser();
sv_param.add(new BasicNameValuePair("Email", un.getText().toString()));
Log.d("un.getText().toString()", un.getText().toString());
sv_param.add(new BasicNameValuePair("Password", pw.getText().toString()));
Log.d("pw.getText().toString()", pw.getText().toString());
String sv_json = sv_jparse.makeHttpRequest(url_lin, "GET", sv_param);
Log.d("sv_json", sv_json);
try {
JSONObject sv_jobj = new JSONObject(sv_json);
if (sv_jobj != null) {
String user_key=sv_jobj.getString("UserID");
log_chk=true;
lgMan.createSession(un.getText().toString(), pw.getText().toString(), user_key);
Intent i=new Intent(Main.this,HomeActivity.class);
startActivity(i);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pDialog.dismiss();
if(!log_chk)
{
AlertDialog.Builder ad= new AlertDialog.Builder(new ContextThemeWrapper(Main.this, R.style.popup_theme));
ad.setTitle("Login Failed");
ad.setMessage("Please Check Your Entry..,");
ad.setInverseBackgroundForced(false);
ad.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
un.setText("");
pw.setText("");
}
});
ad.show();
}
}
}
}
Now you can easily check the internet status by my above class..,
Thank You!
Please Leave Your Comment..,
Have A Happy Day..,
No comments:
Post a Comment