1

I want to check for particular update when user each time connects to internet.I tried the following codes :

unfortunately app is getting stopped while checking for network

Broadcast receiver for checking internet connection in android app

Manifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<receiver android:name=".UpdateReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

Java

public class UpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) 
        context.getSystemService(Context.CONNECTIVITY_SERVICE );
        NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();   
        if (isConnected)       
            Log.i("NET", "connecte" +isConnected);   
        else Log.i("NET", "not connecte" +isConnected);
    }
}

My problem is these codes only receive only once , If the user disconnect wifi and reconnects the BroadcastReceiver unable to notify.Any help is appreciated .

Community
  • 1
  • 1
Sigma Equties
  • 67
  • 2
  • 10

1 Answers1

0

Once I had implemented a check in my app, to see if there is any internet connection or not. You can have a look at this --

public class ConnectionDetector {

private Context context;

public ConnectionDetector(Context cont){
this.context = cont;
}

public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
   NetworkInfo[] info = connectivity.getAllNetworkInfo();
   if (info != null) {
   for (int i = 0; i < info.length; i++) {
   if (info[i].getState() == NetworkInfo.State.CONNECTED) {
        return true;
    }
}
}

}
return false;
}
}

You can initialize an object of this class in your OnCreate method.

And finally call this class' method just before you upload files.

Boolean isInternetConnected = cd.isConnectingToInternet();
if (isInternetConnected)
{
    //perform your job here.
}

EDITED::

NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();

Hope this helps.

Saumik Bhattacharya
  • 891
  • 1
  • 12
  • 28
  • brother i need to identify ( receive ) notification when user reconnects to internet now i am using a timer with interval 10 minutes i want to avoid timer and need when user reconnects to internet – Sigma Equties Mar 14 '16 at 19:05
  • @SigmaEquties -- In that case, you can probably change the return statements to return whenever it represents the first connected network interface or null if none of the interfaces are connected. I have modified my answer above. Please check the edited section. – Saumik Bhattacharya Mar 15 '16 at 10:31