I am using eclipse with android sdk 3.2
I have some problems to make my broadcast receiver working when the connection is lost.
first I have checked the type of network to make sure I understand well :
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
boolean is3g = manager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting();
boolean isWifi = manager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting();
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null)
Toast.makeText(getApplicationContext(), info.getTypeName(),
Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Pas de connexion",
Toast.LENGTH_LONG).show();
if (!is3g)
Log.i("Network Listener", "DISCONNECTED");
else
Log.i("Network Listener", "CONNECTED");
== this is a mobile network, I'm connected
Then I press F8 or I make :
telnet localhost 5554
gsm data off
to stop the connection
Here is my dynamic broadcast receiver in an activity :
public class ActivityA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.query);
this.registerReceiver(this.networkStateReceiver, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
}
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//i have tried several things : State networkState = networkInfo.getState();
// if (networkState.compareTo(State.DISCONNECTED) == 0) ...
if (networkInfo != null && networkInfo.isConnected()) {
Toast.makeText(getApplicationContext(), "CONNECTED",
Toast.LENGTH_LONG).show();
Log.i("Network Listener", "Connected");
} else {
Toast.makeText(getApplicationContext(), "DISCONNECTED",
Toast.LENGTH_LONG).show();
Log.i("Network Listener", "Network Type Changed");
Intent offline = new Intent(AccountInfoActivity.this,
OfflineWorkService.class);
startService(offline);
}
};
My manifest :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
The problem is : when the activityA is launched, my broadcast receive something and it displays "Connected"(Log) and my toast. When I turn off the connection manually, nothing happend. My service is not started, log message are not displayed, only toast messages work ...
And even better, when I turn on the connection again (by pressing F8), I test again the type of connection, Toast messages are shown but this time Log messages don't work.
Problems happend when I press F8. Anyway, I think I miss something with broadcast receivers, it's not totally clear.
Please help me.