0

My android application based on network connection, it works fine when my mobile is connected to internet but when internet connection disconnected it stops working(obesely), did not connect automatically.

And i want to the data onto the server whenever internet available.

Any help would be helpful for me.

NetworkBroadcastReceiver.java

public class NetworkBroadcastReceiver extends BroadcastReceiver {
private static ConnectivityManager mConnectivityManager;

public NetworkBroadcastReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    if (isNetworkAvailable(context))
        Toast.makeText(context, "Connected", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG).show();
}

/**
 *
 * @param context
 * @return
 */
public static boolean isNetworkAvailable(Context context) {
    boolean isMobile = false;
    boolean isWifi = false;

    NetworkInfo[] infoAvailableNetworks = getConnectivityManager(context).getAllNetworkInfo();

    if (infoAvailableNetworks != null) {
        for (NetworkInfo network : infoAvailableNetworks) {

            if (network.getType() == ConnectivityManager.TYPE_WIFI) {
                if (network.isConnected() && network.isAvailable())
                    isWifi = true;
            }
            if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
                if (network.isConnected() && network.isAvailable())
                    isMobile = true;
            }
        }
    }

    return isMobile || isWifi;
}

/**
 * Return connectivity manager instance.
 * @return mConnectivityManager
 */
public static ConnectivityManager getConnectivityManager(Context context) {

    if (mConnectivityManager == null) {
        mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    return mConnectivityManager;
}

}

Manifest.xml

<receiver
        android:name=".receiver.NetworkBroadcastReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
        </intent-filter>
    </receiver>
Vin
  • 109
  • 1
  • 2
  • 7
  • 1
    use broadcast receiver to capture internet connectivity – WISHY Apr 21 '15 at 08:12
  • i'm using broadcast but don't know how to connect, i'm uploading my broadcast code. – Vin Apr 21 '15 at 08:13
  • Did you declared it in the manifest. Refer this http://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app – WISHY Apr 21 '15 at 08:15
  • once you get the broadcast for internet connectivity start a service to send your data to server, after data is submitted to the server stop the service. – WISHY Apr 21 '15 at 08:17
  • 1
    As I can see, your `BroadcastReceiver` is an anonymous class, so there is no way you can declare it in Manifest. Please provide full code of your `Activity` and AndroidManifest.xml – Dmitry Zaytsev Apr 21 '15 at 08:37
  • Make sure that you have corresponding permissions (I don't remember which ones exactly, you can check documentation) – Dmitry Zaytsev Apr 21 '15 at 09:12

1 Answers1

0

to check for network state, make sure that you have declared the ACCESS_NETWORK_STATE permission in your manifest

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Genevieve
  • 450
  • 3
  • 9
  • is your broadcast receiver not being called at all? or is it the isNetworkAvailable method which is not working correctly? if it's problem with the broadcastreceiver not getting broadcast, try including the whole package name in the manifest android:name="com.example.receiver.NetworkBroadcastReceiver" – Genevieve Apr 21 '15 at 11:09