Reputation: 2839
I'm developing an application which check internet is available or not...
I'm getting help from this.
Here is my connection class :
public class ChechConnection {
private Context _context;
public ChechConnection(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;
}
}
And I am using this code for checking : This Is Code For Recharge Activity Class
ChechConnection cDetactor;
Boolean isInternetPresent = false;
If anyone click on a button it should display something if there is an Internet connection.
cDetactor=new ChechConnection(getApplicationContext());
isInternetPresent = cDetactor.isConnectingToInternet();
btn_recharge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isInternetPresent){
Toast.makeText(mContext,"Button Pressed",Toast.LENGTH_LONG).show();
}
else
alert.showAlertDialog(mContext,"Check Connection","Check Your Connection Setting",false);
}
});
This is my own dialog manager :
public class ALertDialogManager {
public void showAlertDialog(final Context context, String title, String message,
Boolean status) {
final Dialog alertDialog = new Dialog(new ContextThemeWrapper(context, android.R.style.Theme_Translucent));
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setCancelable(true);
alertDialog.setContentView(R.layout.dialog);
alertDialog.setTitle(title);
Button ok=(Button) alertDialog.findViewById(R.id.btncancel);
Button cancel=(Button) alertDialog.findViewById(R.id.btnsearch);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Activity activity=(Activity) context;
activity.finish();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
}
But if there is an Internet connection, it gives me an error. Please check my logcat value :
04-29 11:26:15.011: E/AndroidRuntime(2177): Process: com.example.lifegoal, PID: 2177
04-29 11:26:15.011: E/AndroidRuntime(2177): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.lifegoal.eshop.helper.ALertDialogManager.showAlertDialog(android.content.Context, java.lang.String, java.lang.String, java.lang.Boolean)' on a null object reference
04-29 11:26:15.011: E/AndroidRuntime(2177): at com.lifegoal.eshop.Recharge_Activity$1.onClick(Recharge_Activity.java:51)
but if there is an Internet connection, it goes to else part and gives me that logcat value
Thanks!
Upvotes: 0
Views: 2517
Reputation: 339
This method works fine:
public boolean checkInternetConnection(){
ConnectivityManager connec =
(ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
// Check for network connections
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
// if connected with internet
return true;
} else if (
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
return false;
}
return false;
}
And dont forget about AndroidManifest.xml
. You have to define permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Good luck with coding).
Upvotes: 0
Reputation: 1120
It is very common problem, Every time follow line by line tutorials. I think you may forgot initialization of your variable cDetactor. And always follow coding standers so you will eliminate your mistakes like this.
Upvotes: 1
Reputation: 5595
Create a class NetworkInformation.java
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkInformation {
private static NetworkInfo networkInfo;
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
try{
networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
} catch (Exception e) {
e.printStackTrace();
}
// test for connection for WIFI
if (networkInfo != null
&& networkInfo.isAvailable()
&& networkInfo.isConnected()) {
return true;
}
networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// test for connection for Mobile
if (networkInfo != null
&& networkInfo.isAvailable()
&& networkInfo.isConnected()) {
return true;
}
return false;
}
}
Now use the class to check internet present or not by the following code:
if(NetworkInformation.isConnected(Login.this))
{
//your code
}else{
Toast.makeText(Login.this,"No network connection",Toast.LENGTH_LONG).show();
}
Also dont forget to define the following permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Upvotes: 0
Reputation: 3916
use this method :
public static boolean isDeviceOnline(Context context) {
boolean isConnectionAvail = false;
try {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if(netInfo != null)
return netInfo.isConnected();
else
return isConnectionAvail;
} catch (Exception e) {
e.printStackTrace();
}
return isConnectionAvail;
}
Upvotes: 1
Reputation: 2604
ConnectivityManager connMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean connection_check = isConnectingToInternet();
if (connection_check) {
newuser.setEnabled(false);
spinner.setVisibility(View.VISIBLE);
}
else
Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT)
.show();
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) 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;
}
I think this. can help you
Upvotes: 0