I am new using Service
in Android and I am quite confused. I have and Activity
and I want to check for all the time that the network connection is up (wifi or 3g). I implemented a simple Service
and a BroadcastReveiver
.
here the code of the Service
public class NetworkCheck extends IntentService {
private final String CONNECTION = "connection";
public NetworkCheck(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent workIntent) {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
String dataString = workIntent.getDataString();
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
// notify user you are not online
Intent localIntent = new Intent().putExtra(CONNECTION, 0);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
here the code of the BroadcastReceiver
public class NetworkReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int value = intent.getIntExtra("connection", 0);
if(value == 0){
//now I print, then I will show up a dialog
System.out.println("NO CONNECTION!!!!!!!!");
}
}
}
this is the user permission in the Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
and here there is the code that I put in the onCreate
method of my Activity
this.intent = new Intent(this, NetworkCheck.class);
this.startService(this.intent);
LocalBroadcastManager.getInstance(this).registerReceiver(new NetworkReceiver(), new IntentFilter());
This does not work and I cannot figure out why.
Isn´t recommended to check all the time connectivity, call it only when you need it:
WiFi:
public boolean isConnectedWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return networkInfo.isConnected();
}
3G:
public boolean isConnected3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return networkInfo.isConnected();
}
The IntentFilter
that you are registering in your activity doesn't have any sort of filters associated with it.
Since you are creating an intent with the action of "connection" (your CONNECTION
constant), you need to create an IntentFilter with the same action for your BroadcastReceiver to be triggered.
For example:
LocalBroadcastManager.getInstance(this).registerReceiver(
new NetworkReceiver(), new IntentFilter("connection"));
Have you added below intent filter of broadcast receiver.
<receiver android:name=".BroadcastReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
This for your checking your network connection.
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
return (netInfo != null && netInfo.isConnected());
}
It appears your test to get the current network status is incorrect. This test will return false if either WIFI or Mobile data is not available.
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED)
If you are looking to see if network data is available, then take a look at how official documentation handles the test. You could replace your test with something like:
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();