0

I want to get notified when the device connects or disconnects from the internet to perform update.I've put this code in the manifest file :

   <receiver android:name=".Conectivity" android:enabled="true" android:exported="true">
            <intent-filter>
                  <action android:name="android.net.conn.CONNECTIVITY_CHANGE"></action>

            </intent-filter>

And here is my Conectivity class:

package com.funny.pack;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Conectivity extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {

        Log.w("DHA", "Ceva e diferit la conectivitate");

    }

}

Horewer i am not notified when the device connects or disconnects from the internet I think this broadcast receiver must be implemented only from code is that right ? Is there a list of broadcast receivers that cand be implemented from the manifest file and vice versa ?

opc0de
  • 11,557
  • 14
  • 94
  • 187
  • possible duplicate of [Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data](http://stackoverflow.com/questions/5165099/android-how-to-handle-change-in-network-from-gprs-to-wi-fi-and-vice-versa-whi) – waqaslam Jun 06 '12 at 13:46
  • did you add the required permission ? – waqaslam Jun 06 '12 at 13:50
  • And, if you are running on Android 3.1 or higher, do you also have an activity, that you have run at least once, before trying to receive such broadcasts? – CommonsWare Jun 06 '12 at 14:14
  • check this SO page for [Check internet connection using receiver](http://stackoverflow.com/questions/3767591/check-intent-internet-connection) or this tutorial may help you [How to monitor network connectivity](http://thiranjith.com/2011/03/31/how-to-monitor-network-connectivity-in-android/) in Android using Broadcast Receiver let me clear if you still get any trouble. – swiftBoy Jun 06 '12 at 13:48
  • @I don't have trouble implementing the recieiver I am only asking if I can implement the receiver from the manifest file.I've got it working from java code. – opc0de Jun 06 '12 at 13:53
  • yes!! you can register Broadcast Receiver either in Manifest file or in Java code, if you want Broadcast Receiver for all time then go with Manifest entry option or if you want to control manually then Register in java code is better way. – swiftBoy Jun 06 '12 at 13:58

1 Answers1

1

Following Snippet Will Help You.

Create a BroadcastReceiver that will handle connectivity status notifications

public class MainActivity extends Activity {
    // rest of the code in the Activity are committed for clarity
        private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
                NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

                // do application-specific task(s) based on the current network state, such
                // as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
            }
        };

     registerReceiver(mConnReceiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    }

Include

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in manifest

Vipul
  • 27,808
  • 7
  • 60
  • 75