27 May 2017
VAYUZ_The App Killer

The App Killer !

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, RIGHT?

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 Exception?

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? The 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 a 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 exceptions. Typically there are two ways of going about it.

Let me start with 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 an 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 we (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 hello@vayuz.com.

Till we meet again, keep shining!

You’ve been hit by smooth criminal….Long Live MJ!

About Philomathes Jigyasu

Philomathes (pronounced as fillo-MAY-thus) is a fictional character at VAYUZ (https://www.vayuz.com), who is on a never ending journey called “LEARNING”. In a way, Philomathes embodies VAYUZ - Way of life, which is if you are not learning then you are not breathing. The word Philomathes, comes from the Greek roots philo and philein meaning "to love" and the Greek roots mathos (MAH-thos) and mathesis (muh-THAYSIS) meaning “learning”. Philomathes through his Blogs and Vlogs (Video Blogs) will share his experience, learnings and thoughts. In his tryst to learn and understand, he would also seek answers to questions. So if you would like to join him in this incredible journey called “ life” then feel free to write to him on philomathes.jigyasu@vayuz.com. Always remember, knowledge is all around us, we just need to keep our guards down and senses on.

Leave a Comment