Android Basics – Learn how to use Android Notification Service

In order to send Notification to the user you can use the following steps, which have been explained in the video.

Create a Pending Intent

Intent intent = new Intent(this, MainActivity.class);

TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);

PendingIntent pendingIntent = taskStackBuilder.
        getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

 

Create a Notification object

Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(getString(R.string.app_name))
        .setContentText("Sample Notification")
        .setAutoCancel(true)
        .setPriority(Notification.PRIORITY_MAX)
        .setDefaults(Notification.DEFAULT_VIBRATE)
        .setContentIntent(pendingIntent)
        .build();

Use NotificationManager to send the Notification

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);

Source Code available here

Android Basics : Learn how to use a Handler with IntentService to update the Main Thread

In the last Article you saw how to call an IntentService. Then you printed a Log statement in the background thread, once the service completed.

If you want to display a message in the Main Thread then you can use a Handler. A Handler allows you to access the Main Thread once the service is complete.

Here is the sample source code to achieve this.

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v(TAG, "In onHandleIntent. Message will be printed after 10sec");
        if (intent != null) {
            synchronized (this){
                try {
                    wait(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.v(TAG, "Service started");
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Service Completed" , Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

Complete source code is available here.

I am not a visionary. I’m an engineer – Linus Torvalds

If you are into Computers and haven’t heard of Linus Torvalds then you better do some lookup. To start with he gave us Linux and to add icing to the cake he created Git.

Recently I heard him talk in a TED interview and thought of sharing it. It’s a talk worth listening to. He is not a people’s person and likes to work in his quiet office, all by himself.

The part which sounded really interesting was his take on Visionaries and why he identified more with Edison than with Tesla.

I am not a visionary. I do not have a five-year plan, I don’t have a moon shot. I’m an engineer.

Listen him share his opinions.

Android Basics – Learn how to create a Started Service using an IntentService

In the last Article you learnt about Android Services and the difference between a bound and a started service. In this Article you will learn how to call the IntentService you created earlier.

To create a started service you need to extend the IntentService class.

public class SampleService extends IntentService

Let’s take a simple example to illustrate how this works.

In the onHandleIntent method you will add a Log.

@Override
protected void onHandleIntent(Intent intent) {
    Log.v(TAG, "In onHandleIntent. Message will be printed after 10sec");
    if (intent != null) {
        synchronized (this){
            try {
                wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.v(TAG, "Service started");
        }
    }
}

To call this service you will add a button to the content_main.xml file

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_margin="15dp"
    android:id="@+id/callSampleServiceId"
    android:text="Start Service"
    android:background="@android:color/holo_blue_dark"
    android:textColor="@android:color/white"/>

On click of this button an explicit intent will be called.

if(v.getId() == R.id.callSampleServiceId){
    Log.v(TAG, "Service Button Clicked");

    Intent intent = new Intent(this, SampleService.class);
    startService(intent);
}

View the complete source code here.

Android Basics – Introduction to Services

A Service helps you to execute tasks in the background. Like downloading a file or tracking user location changes.

It is an Application Componenet with no user interface. It has a simple life cycle and comes with a set of features.

There are two types of Services

Bound Service  This kind of service is bound to a component like an Activity and runs as long as the Activity runs.

Started Service  This kind of service can run indefinitely. Even when the Activity that started is is destroyed.

You can create a new service by extending either the Service Class or the IntentService Class.

IntentService allows you to create a started service.

In the video you will learn how to get started by creating an IntentService.

Source Code is available here