There is a method that checks if there is an internet connection.
How to implement waiting for an Internet connection (with the message that the Internet is not available)?
Further code should not be executed until the Internet appears.
Look towards the receiver and the intents of changing the connection status.
Here you can look at the sample code.
Here for example BroadcastReceiver
public class NetworkChangeReceiver extends BroadcastReceiver { private static String TAG = NetworkChangeReceiver.class.getSimpleName(); @Override public void onReceive(final Context context, final Intent intent) { Log.e(TAG, "Network Connection is [" + isNetworkConnectedOrConnecting(context) + "]"); } public boolean isNetworkConnectedOrConnecting(Context context) { final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnectedOrConnecting(); } } and the method that you need to register in atikivti and call it:
private void initBroadcastReceiver() { NetworkChangeReceiver receiver = new NetworkChangeReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiver(receiver,filter); } Those. if there is a network connection, the log will be Network Connection is [true]
Source: https://ru.stackoverflow.com/questions/517785/
All Articles