Chunibabu aaj kaha phaas gaye…well this is one joke, which everyone @ VAYUZ can relate to.

English Translation (not exact) would be something like….Where do you have your neck stuck today?

The most obvious adage in the world of development. Most of you would agree, Is not it?

While developing an app, you constantly face new challenges every other day. At times, if it’s too easy then you feel that something is wrong. So today let’s look at one of the bigger challenges in product development — Identifying Exceptions.

Why Exceptions?

Well typically, the last few weeks of the development are lost just identifying and closing exceptions. We also call these exceptions — Bleeding Point. Presence of exceptions in your app / product can leave a huge dent on user experience. So even the best of product would fall flat on its face if these exceptions are not handled aptly.

As per our Center of Excellence Framework, we focus on identifying these exceptions during the documentation phase. But can you identify them all in the beginning?

Well no! That’s why it makes perfect sense to capture these exceptions during modular level UATs.

So today let me touch upon a very important element, absence of which can put your App into limbo.

What’s that?

INTERNET !!!

Have you ever been in a situation where you are showcasing your App to someone and you are frantically clicking on a button but nothing is happening. Next thing you get to hear is, “this **** ain’t working, looks like we have a dud of an App”. Suddenly all those lines of code seem to be waste of everyone’s time and you have just triggered a perfect storm.

So you go back, dig in and then you realize it was the dropping / intermittent internet, which had sponsored your last 60 mins of “you don’t know how to code” session. Well ! you can’t blame anyone cause at the end of the day “results are tangible”.

So let’s look at how can you manage or catch Internet dropping exception. Typically there are two ways of going about it.

Lets start with the the traditional approach, which is also the more popular one used in Android development.

The Traditional Approach

  1. Create a broadcast receiver

public class ConnectivityReceiver
extends BroadcastReceiver {

public static ConnectivityReceiverListener connectivityReceiverListener;

public ConnectivityReceiver() {
super();
}

@Override
public void onReceive(Context context, Intent arg1) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.
CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();

if (connectivityReceiverListener != null) {
connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
}
}

public static boolean isConnected() {
ConnectivityManager
cm = (ConnectivityManager) AppController.
getInstance().getApplicationContext()
.getSystemService(Context.
CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
}

public interface ConnectivityReceiverListener {
void onNetworkConnectionChanged(boolean isConnected);
}
}

  1. Next up, register this in manifest

<receiver
android:name=“.service.ConnectivityReceiver”
android:enabled=“true”>
<intent-filter>
<action android:name=“android.net.conn.CONNECTIVITY_CHANGE”/>
</intent-filter>
</receiver>

  1. Now, in each Activity you have to override a method and typically you will show a snackbar over there that says “No internet connection” like this:

This solution works, but will cause an overhead in case of multiple activities.

The Smooth Criminal Approach

With this, the best part is you don’t have to write the code for internet connectivity in each activity, like we did above. Write once and you are good to go !

  1. Broadcast Receiver code:

public class NetworkReceiver extends BroadcastReceiver {
static Snackbar
snackbar; //make it as global
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.
CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
// Toast.makeText(context,”NO Internet connection”,Toast.LENGTH_LONG).show();
NetworkReceiver.snack(null, 0, “No Internet Connection.”,context.getApplicationContext());
}else{
NetworkReceiver.
hideSnackbar();
}
}

public static void snack (HashMap<String,View.OnClickListener> actions, int priority, String message, Context context) {
if(AppController.
appActivity != null){
snackbar = Snackbar.make(AppController.appActivity.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);//MyApplication.appactivity from Application class.
if (actions != null) {
Iterator iterator = actions.entrySet().iterator();
snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
snackbar.setAction((String) pair.getKey(), (View.OnClickListener) pair.getValue());
iterator.remove();
// avoids a ConcurrentModificationException
}
}
switch (priority) {
case 0:
snackbar.getView().setBackgroundColor(context.getResources().getColor(R.color.black));
break;
case 1:
snackbar.getView().setBackgroundColor(Color.parseColor(“#66ccff”));
break;
case 2:
snackbar.getView().setBackgroundColor(Color.parseColor(“#66ff33”));
break;
}
snackbar.show();
}
}
private static void hideSnackbar(){
if(
snackbar !=null && snackbar.isShown()){
snackbar.dismiss();
}
}
}

  1. Register this in Manifest:

<receiver
android:name=“.helper.NetworkReceiver”>
<intent-filter>
<action android:name=“android.net.conn.CONNECTIVITY_CHANGE” />
</intent-filter>
</receiver>

  1. Now create a Application class and implement that with Application.ActivityLifecycleCallbacks
  2. In onActivityResumed() method, you’ll need to add

appActivity = activity; //here we get the activity
Intent i = new Intent(this, NetworkReceiver.class);
sendBroadcast(i);
//here we are calling the broadcastreceiver to check connection state.

Conclusion

So this is one of those things, which can save you from those App Killer moments. One chunk of code in one location and you are done. Try this out and let me know if this works for you and if it has saved you from those “How bad a coder” sessions then feel free to send me some goodies. Coding is fun but the fun becomes fantastic when you know those smart tweaks. Also remember to pick brains of people with prior experience of launching successful Mobile Apps. Their insights could help you in covering your base well. This is where VAYUZ can help you in your mobile application development journey.

I would love to hear your thoughts on how you are handling app killer exceptions at your end, so leave a comment or drop me a note on pooja.chauhan@vayuz.com.

Till we meet again, keep shining!

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade