Once, when I had a problem, you guys were very helpful. So, I am again with another problem that I fell into ...: /
I am running a custom NetworkReceiver that extends BroadcastReceiver. I want to determine when the phone has an Internet connection so that I can start the service when this happens. I read a lot of topics and all topics will more or less tell the following code, which DOES NOT WORK for me:
public class NetworkReceiver extends BroadcastReceiver {
private static final String tag = NetworkReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(tag,"****** NetworkReceiver // onReceive()");
boolean noConnection = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.i(tag,"****** NetworkReceiver // Network Down?: " + intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false));
}
if(noConnection) {
Log.i(tag,"****** NetworkReceiver // Stopping Service...");
context.stopService(new Intent(context, FetchService.class));
} else {
Log.i(tag,"****** NetworkReceiver // Starting Service!");
context.startService(new Intent(context, FetchService.class));
}
}
, logcat NetworkDown?: false, ( ). , F8, dev- ( SIM-, Wi-Fi-), → !
:
...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".NetworkReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
...
,
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" /> , :
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true)
- .
, Extras Intent , getBooleanExtra() .
PS. Android 2.1
!