-1

Helle Stack community,

I created a simple app that loads a json and loads the data in a recyclerview.
The recyclerview includes cardviews.

The activity shows me a blank page if I haven't any internet connection, but normally I want to see some blank cardviews like in the 9GAG app.
In the 9GAG app you get all data on swipe the display.
My app should load all data on internet connection is available. I googled something about broadcast receiver, but can't find a simple example for my need.

Maybe someone can show me a simple example or the way to do some action like in the 9GAG app. Info: The app is for api 21 User.
I would appreciate it.

Paul Nie
  • 21
  • 5
  • http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-times-out – bichito Mar 28 '17 at 23:20

2 Answers2

0
public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (Network.isNetworkAvailable(Constants.ApplicationContext)) {

        } else {

        }
    }


}
public class Network {
public static boolean isNetworkAvailable(Context context) {
    int retriesNum = Constants.checkConnectionRetriesNum;// a number that I put as 5 for retries to make consideration for bad connections
    if(context!=null)
    while (retriesNum > 0) {
       try {
           ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
           NetworkInfo network = connectivityManager.getActiveNetworkInfo();
           boolean isConnected = network != null &&
                   network.isConnectedOrConnecting();
           if (isConnected) {
               return true;
           } else {

               network = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
               isConnected = network != null && network.isConnectedOrConnecting();
               if (isConnected) {
                   return true;
               } else {
                   network = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                   isConnected = network != null && network.isConnectedOrConnecting();
                   if (isConnected) {
                       return true;
                   } else {
                       retriesNum--;

                   }
               }
           }
       }catch (Exception ex){
           return true;
       }
    }
    return false;
}
Daniel Raouf
  • 2,307
  • 1
  • 22
  • 28
0

Use a BroadcastReceiver that gets notified, when the connection state changes. It is very important that you register it in your manifeset!

Add a class, similar to this to your project:

public class NetworkChangedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Network state changed!
        // Check if the user connected using ConnectivityManager.getActiveNetworkInfo()
    }
}

Add it to your manifest file, so the systems knows it should notify you:

<manifest>
    ...
   <application>
       ...
       <receiver
            android:name=".NetworkChangedReceiver"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
       </receiver>
       ...
    </application>
</manifest>

Edit: From the docs:

Apps targeting Android 7.0 (API level 24) do not receive CONNECTIVITY_ACTION broadcasts if they register to receive them in their manifest, and processes that depend on this broadcast will not start. This could pose a problem for apps that want to listen for network changes or perform bulk network activities when the device connects to an unmetered network. Several solutions to get around this restriction already exist in the Android framework, but choosing the right one depends on what you want your app to accomplish.

Note: A BroadcastReceiver registered with Context.registerReceiver() continues to receive these broadcasts while the app is running.

I didn't knew about this, so thanks to @Paul Nie for letting me know :D

I can't really help further at this point. But this seems like a good point to start some research about this topic: https://developer.android.com/topic/performance/background-optimization.html#connectivity-action

Vast Dan
  • 74
  • 4