I know many will find this question not worth but i am new to android development.I want to know regarding services and broadcast receivers.I have viewed some demo apps on broadcast receiver and made few demo apps.Now what i am trying to accomplish i need help. I want to create a service which runs in background and it will tell the user regarding the network or wifi available or not.Please do help me out.Thanks
Asked
Active
Viewed 380 times
2 Answers
0
You could use a broadcast receiver
<receiver
android:name=".NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
In you Receiver class
public class NetworkChangeReceiver extends BroadcastReceiver {
public static Internet internet;
public static String changestatus = "";
@Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
if (status.equals("Not connected to Internet")) {
if (context != null && internet != null) {
internet.net();
}
}
changestatus = status;
}
public interface Internet {
public void net();
}
}
Network Util class to check connectivity
public class NetworkUtil {
public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;
public static int getConnectivityStatus(Context context) {
if (context==null) {
return 0;
}
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}
public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = "Mobile data enabled";
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = "Not connected to Internet";
}
return status;
}
}
In your Activity you could implement the net interface
public class MyActivity extends FragmentActivity implements
Internet {
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
NetworkChangeReceiver.internet = this;
}
@Override
public void net() {
// TODO Auto-generated method stub
//Show Alert connection is not avaiable
}
}

George Thomas
- 4,566
- 5
- 30
- 65
-
no..if connection gets lost the broadcast gets called and the interface will push the status to the activity and you can check it there and display the apt message in net() method – George Thomas Mar 31 '15 at 10:31
-
But what if the user terminates the app,then how will i come to know abt netwirk change? – Anuj Mar 31 '15 at 10:37
-
1why do you wanna check for connectivity if your app is closed? – George Thomas Mar 31 '15 at 11:13