Reputation: 5083
I am trying to create button like this (If mobile network is turned on it is green, when it is off, it is grey):
I am trying to identify state using this method:
public Boolean isDataNetworkEnabled() {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
return true;
}
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
return false;
}
return false;
}
It always returning value 'false'. Even if after mobile network turned on. Actually, how to identify state of mobile network?
PS. I am not asking about how to turn on/off mobile network, I am asking about state. I have already found how to turn on/off:
try {
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
if (!isDataNetworkEnabled()) {
dataMtd.invoke(dataManager, true);
ivDataNetwork.setImageResource(R.drawable.ic_data_usage_black_48dp);
Toast.makeText(context, "Data network turned on", Toast.LENGTH_SHORT).show();
} else {
dataMtd.invoke(dataManager, false);
ivDataNetwork.setImageResource(R.drawable.ic_data_usage_grey600_48dp);
Toast.makeText(context, "Data network turned off", Toast.LENGTH_SHORT).show();
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Upvotes: 0
Views: 715
Reputation: 6109
Replacing some values...
if ( conMgr.getNetworkInfo(TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(TYPE_WIFI).getState() == NetworkInfo.State.CONNECTING) {
return true;
}
if ( conMgr.getNetworkInfo(TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) {
return false;
}
So you are really checking if mobile data is connected, or wifi is connecting. It sounds like you don't want wifi, so change TYPE_WIFI
to TYPE_MOBILE
?
In fact it might be slightly better to do:
public boolean isMobileDataNetworkAccessable() {
return conMgr.getNetworkInfo(NetworkType.TYPE_MOBILE).isConnectedOrConnecting();
}
Upvotes: 0
Reputation: 1560
Best practice is to use broadcast receiver
Register reciever under in manifest.xml
<receiver android:name="com.example.broadcastreciever2.NetworkChangeReciever" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Also add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Define your reciever ( Use according to your use )
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class NetworkChangeReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (checkInternet(context)) {
Toast.makeText(context, "Network Available Do operations",
Toast.LENGTH_LONG).show();
}
}
boolean checkInternet(Context context) {
ServiceManager serviceManager = new ServiceManager(context);
if (serviceManager.isNetworkAvailable()) {
return true;
} else {
return false;
}
}
}
Define ServiceManager by extending ContextWrapper
import android.content.Context;
import android.content.ContextWrapper;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ServiceManager extends ContextWrapper {
public ServiceManager(Context base) {
super(base);
}
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
}
Upvotes: 1