Android Progress Notification with Examples,In android, Progress Notification is used to reveal the progress of an ongoing operation in notification bar.
By using development notification, we can effortlessly know that how an awful lot percentage of the current operation finished and how long the operation will run to finish the closing operation.
In android, varieties of development signs to be had, one is determinate and every other one is indeterminate.
Eventhough ,If we are acknowledged approximately how long the operation will take to finish, then we will use determinate shape of the indicator.
Android Progress Notification with Examples
Generally, the progress signs in android are applied with the aid of using ProgressBar magnificence.
Because, to display the development signs in our app, we want to feature the progress bar to our notification by calling setProgress(max, progress, false) approach and then trouble the notification.
Commonly, Here the 0.33 argument in setProgress() technique is used to signify whether or not the progress bar is determinate (false) or indeterminate (authentic).
As our operation proceeds, we need to growth the cost of development, and replace the repute of notification. At the quit of operation, the progress value must be same to max price.
The higher way to name setProgress() is to set max value to one hundred after which increment development as a percent complete price for the operation.
At first, Once the operation is accomplished leave the development bar displaying or eliminate it by using calling setProgress(0,0, false) and update the notification textual content to expose that the operation is complete.
Now we will see a way to create and display the development bar in android notification bar with examples.
Android Progress Notification Example
Create a brand new android application using android studio and give names as ProgressNotification.
Now open an activity_main.Xml file from reslayout path and write the code like as shown below
activity_main.Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification"
android:layout_marginTop="100dp" android:layout_marginLeft="120dp"/>
</LinearLayout>
Although, If you observe above code we created a one Button manipulate in XML Layout record to expose the development indicator in notification while we click on on Button.
Thogh, Once we’re finished with advent of layout with required controls, we want to load the XML format useful resource from our hobby onCreate() callback approach, for that open foremost hobby report MainActivity.
Java from javacom.Tutlane.Progressnotification course and write the code like as proven underneath.
MainActivity.java
package com.tutlane.progressnotification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private NotificationManager mNotifyManager;
private NotificationCompat.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.btnShow);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(MainActivity.this);
mBuilder.setContentTitle("File Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.download);
// Start a the operation in a background thread
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
// Do the "lengthy" operation 20 times
for (incr = 0; incr <= 100; incr+=5) {
// Sets the progress indicator to a max value, the current completion percentage and "determinate" state
mBuilder.setProgress(100, incr, false);
// Displays the progress bar for the first time.
mNotifyManager.notify(id, mBuilder.build());
// Sleeps the thread, simulating an operation
try {
// Sleep for 1 second
Thread.sleep(1*1000);
} catch (InterruptedException e) {
Log.d("TAG", "sleep failure");
}
}
// When the loop is finished, updates the notification
mBuilder.setContentText("Download completed")
// Removes the progress bar
.setProgress(0,0,false);
mNotifyManager.notify(id, mBuilder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();
}
});
}
}
If you examine above code we are created a progress notification the use of setProgress() method and showing the development notification on Button click.
Output of Android Progress Notification Example
Because,When we run above example using android virtual device (AVD) we will get a result like as shown below.
If you look at above end result we created a development notification and shown it on Button click based totally on our requirements.
This is how we will use progress notification in our android packages based on our requirements.