By   September 3, 2014

This is continuation of our post for android app development for viewing GitHub contributions for a user. As we have been going along this path we have been adding different features to the app learning the details about the development of the feature in consideration. In this post we are going to add toast notifications and vibrations when our android device is connected / disconnected from the network.

Toasts

Toasts are also notification messages which are shown to the user for a limited time. They are mostly status updates with minimum UI. Generally we just set the message and show it but we can also customize the view keeping them close to the theme of our app.

The toast would be displayed when the application is up and running and suddenly there is a disconnect from the network. The toast message should provide the details about the disconnect.

toast_displaed

A toast can be displayed for a long or short duration. Unfortunately there are only two constant values which can be specified. They are Toast.LONG_DURATION and Toast.SHORT_DURATION. They have values of 1 and 0 respectively. Now what would be the actual duration when we specify one of these values? For Toast.LONG_DURATION, the actual time this message would continue to be displayed is 3500ms (3.5 seconds). For Toast.SHORT_DURATION it is 2000ms (2 seconds).

We can also customize the display of the toast notification using setView method. It accepts android.view.View as argument.

Vibrating Android Device

As you know Android devices also supports vibrations. It supports vibrating a device for a limited duration or indefinitely. It also supports patterns for vibrations where a device is vibrated including various delays in between.

In order to access the vibration service, we need to add the required permission requirement in AndroidManifest.xml as follows:

Below we are using Android API to get Vibrator Service. We are checking if android service is available for our app plus device supports vibrating. We are then vibrating the device for a certain duration (500 ms).

An Android emulator doesn’t supports vibrating the virtual device. Now how can we test if the vibration code is working. Actually we can just have a look at the LogCat messages. Here it shows the vibrating service being used.

vibrator_logcat

Download Code

AndroidApp_GitHubDownload