Use Notification in Android to implement the progress notification bar (Example 3) and the Notification progress bar

Source: Internet
Author: User

Use Notification in Android to implement the progress notification bar (Example 3) and the Notification progress bar

When we use the APP, the software occasionally prompts us to update the version. After we click "Confirm Update", the download and update progress will be displayed in the notification bar (Progress bar of known length) And the installation status (Uncertain progress bar. The implementation result is as follows:

Before implementing the Code function, we should first explain the two statuses of the progress bar:

(1) display a progress bar indicator with a known length (Displaying a fixed-duration progress indicator)

To display a definite progress bar, call setProgress () setProgress (max, progress, false) to add a progress bar to your notification. Then release the notification. Then, as the operation progresses, increase the progress value and update the notification. At the end of the operation, the Progress value should be equal to the maximum value. The common method is to call setProgress () to set the maximum value to 100, and then increase the percentage of progress completion. You can display the progress bar when the operation is complete, or remove it. In this case, remember to update the text of the notification and the operation is complete. Call setProgress (0, 0, false) to remove the progress bar.

Public Builder setProgress (int max, int progress, boolean indeterminate)

Where max is the maximum progress, progress is the current progress, and indeterminate is uncertain (if it is set to true, it is uncertain, otherwise it is determined)

(2) display a continuous activity indicator (Displayinga continue activity indicator)

To use an uncertain activity indicator, use the setProgress (0, 0, true) method to add your notification (the first two parameters are ignored) and then publish the notification. Unless its animation effect is specified, the style of this indicator is the same.

The notification is released at the beginning of the operation. The animation will be executed until you modify the notification. when the operation is completed, setProgress (0, 0, false) is called) to update the notification to remove the activity indicator. We always do this unless you want to complete the operation, the animation effect is still running.

Remember to update the text in the notification when the operation is complete.

With these two points in mind, we start to implement the Code:

In layout, click the trigger button layout:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 xmlns: tools = "http://schemas.android.com/tools" 4 android: id = "@ + id/activity_content" 5 android: layout_width = "match_parent" 6 android: layout_height = "match_parent" 7 tools: context = "com. example. administrator. day12.ContentActivity "> 8 <TextView 9 android: layout_width =" match_parent "10 android: layout_height =" match_parent "11 android: gravity =" center "12 android: textSize = "30sp" 13 android: text = "show progress"/> 14 </LinearLayout>
Java code implements MainActivity. java:
1 import android. app. notification; 2 import android. app. icationicationmanager; 3 import android. app. pendingIntent; 4 import android. content. context; 5 import android. content. intent; 6 import android. graphics. bitmapFactory; 7 import android. support. v7.app. appCompatActivity; 8 import android. OS. bundle; 9 import android. support. v7.app. notificationCompat; 10 import android. view. view; 11 import android. widget. remoteViews; 12 public class MainActivity extends AppCompatActivity {13 // define the ID14 private static final int NO_3 = 0x3; 15 @ Override16 protected void onCreate (Bundle savedInstanceState) {17 super. onCreate (savedInstanceState); 18 setContentView (R. layout. activity_main); 19} 20 public void show3 (View v) {21 final NotificationCompat. builder builder = new NotificationCompat. builder (this); 22 builder. setSmallIcon (R. mipmap. huangyueying); 23 builder. setContentTitle ("Download"); 24 builder. setContentText ("downloading"); 25 final NotificationManager manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); 26 manager. notify (NO_3, builder. build (); 27 builder. setProgress (, 0, false); 28 // download and installation Thread simulation 29 new Thread (new Runnable () {30 @ Override31 public void run () {32 for (int I = 0; I <100; I ++) {33 builder. setProgress (100, I, false); 34 manager. notify (NO_3, builder. build (); 35 // download progress prompt 36 builder. setContentText ("Download" + I + "%"); 37 try {38 Thread. sleep (50); // demonstrate sleep 50 ms 39} catch (InterruptedException e) {40 e. printStackTrace (); 41} 42} 43 // After the download is complete, change the title and prompt information 44 builder. setContentTitle ("START installation"); 45 builder. setContentText ("installing... "); 46 // sets the progress to uncertain, used to simulate the installation of 47 builder. setProgress (0, 0, true); 48 manager. notify (NO_3, builder. build (); 49 // manager. cancel (NO_3); // set to close the notification bar 50} 51 }). start (); 52} 53}

Here we only implement the simulation results in a simple way. In order to let everyone know and use Attribute Methods skillfully, we will make technical reserves for the subsequent real projects.

Reading related documents: Using Notification in Android to implement the normal Notification bar (Notification Example 1) Using Notification in Android (Notification Example 1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.