Reputation: 744
I need to notice the network change in the android system and perform particular activities accordingly.
1) When WiFi is enabled, perform function A. 2) When Mobile network is enabled, perform function B. 3) When WiFi is disabled and Mobile Network is enables, stop function A and start function B. 4) When Mobile Network is disabled and WiFi is enabled, stop function B and start function A. 5) When no network is available, no function is performed.
I tried using CONNECTIVITY_CHANGE. With this, I get the Intent many times, I know its reason too. There is also another problem. Consider that Mobile Network is ON. Now, when I enable WiFi, I get WiFi enabled 2 to 3 times, also, Mobile Network disabled will not be confirmed.
I also tried registering with PhoneStateListener.LISTEN_DATA_CONNECTION_STATE. It almost worked, but, when both WiFi and Mobile Network are on, I know WiFi will be active, and when I switch off WiFi, sometimes I get the Mobile Network enabled message and sometimes I wont.
I need to solve this problem. Please help me with your guidance.
Upvotes: 5
Views: 2895
Reputation: 6021
So actually you have four states as (+fifth if internet is there or not)
when network is available, you have to check for the internet provider by monitoring the states of Wifi and Mobile Network, your four states are
Wifi change states and
1. Mobile network is enabled
2. Mobile network is disabled
Mobile network change states and
1. Wifi is enabled
2. Wifi is disabled
first of all you have to monitor if the internet is available or not, after this you will have to add similar two listeners (Broadcast recievers) for Wifi state change and Mobile state change and check for the required state, I have added state check method below,
Reciever Implementation
Checking for changes in a receiver and then checking state
public class Internet_State extends BroadcastReceiver {
//checked with new state changed when event occurs
public boolean oldInternetState;
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
Bundle b = intent.getExtras();
// if Internet available
boolean isConnected = !b
.getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY);
// ignore if no state change
if (oldInternetState == isConnected)
return;
// set new oldInternetState
oldInternetState = isConnected;
boolean isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
boolean isWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isAvailable();
//you can save these states to be monitored in wifi and mobile change change listners
//No internet is 0 state
int state = isConnected?1:0;
if(state)
state = checkState(context)
intent.putExtra("state", state);
// and then send this intent to your required method which will
// check the state and perform function
}
@Override
public void initialize(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
//save the current internet state at start of the receiver registration
oldInternetState = false;
if (activeNetwork != null)
oldInternetState = activeNetwork.isConnectedOrConnecting();
}
}
Implement two, mobile and wifi state change broadcast recievers and better add a state check method like this
Check State
Monitoring the state out of four states
public int checkState(Context context){
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
boolean isWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isAvailable();
//No internet is 0 state
int state = isConnected?1:0;
if(isWifi && isMobile)
state = 1;
else if(isWifi && !isMobile)
state = 2;
if(isMobile && isWifi)
state = 3;
else if(isMobile && !isWifi)
state = 4;
return state;
}
and call this method from 3 Wifi, Internet and Mobile data change recievers, dont forget to check for oldState and match it with the changed state.
Mobile old state by
NetworkInfo allNetInfo = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
oldDataState = allNetInfo.getState();
for Wifi state
int state = intent.getExtras().getInt(WifiManager.EXTRA_WIFI_STATE);
if (state == WifiManager.WIFI_STATE_ENABLED)
state = 1;
else if (state == WifiManager.WIFI_STATE_DISABLED)
state = 0;
else
return;
if (state == oldWifiState)
return;
oldWifiState = state;
Problem I get WiFi enabled 2 to 3 times
More than one calls on Wifi change occure because Wifi changing states, like TurningOn and TurningOf are triggered by OS, you have to cancel out them, like I did above like
if (state == WifiManager.WIFI_STATE_ENABLED)
state = 1;
else if (state == WifiManager.WIFI_STATE_DISABLED)
state = 0;
else
return;
Only Enabled and Disabled are monitored now.
Upvotes: 1
Reputation: 677
Try this code in your first activity in application.
public class YourActivity extends Activity {
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
this.registerReceiver(this.mNetworkConReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); // Here you register your receiver
}
private BroadcastReceiver mNetworkConReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMngr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isConnected()){
// Wifi is connected
}else if(mobile.isConnected() && wifi.isConnected()){
// Mobile Network and Wifi is connected
}// Others conditions here...
}
};
}
Remember: Your broadcast receiver will'be triggered by Android everytime network state change.
Upvotes: 0
Reputation: 360
Please find the below code. I suppose it is what you want. In case I misunderstood you, then please let me know:
public void checkWifiMobileNetwork(Context context){
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
android.net.NetworkInfo connWifiType = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo connMobileType = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(connWifiType.isConnectedOrConnecting()){
Log.d("NETWORK_STATUS","WIFI_IS_THERE");
}else{
Log.d("NETWORK_STATUS","WIFI_IS_NOT_THERE");
}
if(connMobileType.isConnectedOrConnecting()){
Log.d("NETWORK_STATUS","MOBILE_NETWORK_IS_THERE");
}else{
Log.d("NETWORK_STATUS","MOBILE_NETWORK_IS_NOT_THERE");
}
}else {
//notify user you are not online
Log.d("NETWORK_STATUS","WIFI_MOBILE_IS_NOT_THERE");
}
}
Set permission at Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Upvotes: 1
Reputation: 677
You can try register a Broadcast Receiver to notice network change. See: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html.
Be careful, the state can change too many times and everytime your broadcast will'be triggered.
Upvotes: 0