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 .

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.