Tutorials Android Progress Notification in Android Example

Progress Notification in Android Example

In this example we’ll show you how to display progress notification in Android using NotificationManager class. For the sake of simplicity the below example shows an simple AsyncTask that does the background operation and update the progress bar displayed on android notification area.

As described above this example, using an simple layout with an single button. While user clicking on the button the AsyncTask starts and the it displays the progress notification.

Application Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Start Download" />

</LinearLayout>

Activity Class

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	private NotificationManager mNotifyManager;
	private Builder mBuilder;
	int id = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button b1 = (Button) findViewById(R.id.button1);
		b1.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				mBuilder = new NotificationCompat.Builder(MainActivity.this);
				mBuilder.setContentTitle("Download")
						.setContentText("Download in progress")
						.setSmallIcon(R.drawable.ic_download);

				new Downloader().execute();
			}
		});
	}

	private class Downloader extends AsyncTask<Void, Integer, Integer> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			// Displays the progress bar for the first time.
			mBuilder.setProgress(100, 0, false);
			mNotifyManager.notify(id, mBuilder.build());
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// Update progress
			mBuilder.setProgress(100, values[0], false);
			mNotifyManager.notify(id, mBuilder.build());
			super.onProgressUpdate(values);
		}

		@Override
		protected Integer doInBackground(Void... params) {
			int i;
			for (i = 0; i <= 100; i += 5) {
				// Sets the progress indicator completion percentage
				publishProgress(Math.min(i, 100));
				try {
					// Sleep for 5 seconds
					Thread.sleep(2 * 1000);
				} catch (InterruptedException e) {
					Log.d("TAG", "sleep failure");
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			mBuilder.setContentText("Download complete");
			// Removes the progress bar
			mBuilder.setProgress(0, 0, false);
			mNotifyManager.notify(id, mBuilder.build());
		}
	}
}

Download Source Code

Download Source Code from GitHub.

Output

Displaying Progress Notification in Android Example

Total
0
Shares
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
hanane
hanane
7 years ago

great tuto thank you , do you know how can i do it using phonegap/cordova instead of native android?

javatechig
javatechig
7 years ago
Reply to  hanane

You probably have to search some compatible codova plugins..

shiva
shiva
7 years ago

sir actually when i download some thing in background using your tutorial i will not be able to do any operation on my front end it blocks the UI also.

javatechig
javatechig
7 years ago
Reply to  shiva

If you’re following my tutorial to download using AsyncTask it wont block ui thread. AsyncTask runs on separate thread. By the way what are you downloading ?

Rohan
Rohan
7 years ago

How can I also show the percentage downloaded?

Chintan Rathod
Chintan Rathod
6 years ago

Great tutorial… thanks..

Previous Post
TennisTV Android Application

TennisTV – Best Live Streaming App for Tennis fans

Next Post
Android Tutorials

Android Versions and API Levels

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
6
0
Would love your thoughts, please comment.x
()
x