Use notification to implement progress notification bar in Android (example three)

Source: Internet
Author: User

We use the app in the process, the software will occasionally prompt us to make version updates, we click to confirm the update, the notification bar will show the download update progress ( the known length of the progress bar ) and installation ( uncertainty progress bar ), this is what we want to achieve today. The implementation results are as follows:

Before the code implements the functionality, we will first explain the two States of the progress bar:

(1) Display a progress bar indicator of a known length (Displaying a fixed-duration progress indicator)

To be able to display a definite progress bar, add a progress bar to your notification by calling setprogress () setprogress (max, progress, false). then publish the notification. Then, with the progress of the operation, increase the progress value, and then update the notification. At the end of the operation, the progress value should be equal to the maximum value. The usual way is to call Setprogress () to set the maximum value to 100 and then go to increase the percentage of progress complete. You can display the progress bar when the operation is complete, or you can remove it. In this case, remember to update the text of the notification, and the display operation is complete. Call setprogress (0, 0, false) to remove the progress bar.

Public Builder setprogress (int max, int progress, Boolean indeterminate)

which Max is the maximum progress value, Progress for the current progress, indeterminate is indeterminate (set to true , it is indeterminate, and vice versa)

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

To be able to use an indeterminate activity indicator, use the setprogress (0, 0, True) method to add to your notification (the first two parameters are ignored) and then publish the notification. The indicator's style is the same unless you want to specify its animation effect.

When the action is started, the animation will be executed until you modify the notification, and when the operation is complete, call setprogress (0, 0,false) to update the notification to remove the activity indicator. We always do this, unless you want to get the operation done, the animation effect is still running.

Also, remember to update the text in the notification when the operation is complete.

Knowing these two points, we begin to implement the code:

In layout, click the trigger button:
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:id= "@+id/activity_content"5Android:layout_width= "Match_parent"6android:layout_height= "Match_parent"7tools:context= "Com.example.administrator.day12.ContentActivity" >8<TextView9Android:layout_width= "Match_parent"Tenandroid:layout_height= "Match_parent" Oneandroid:gravity= "Center" AAndroid:textsize= "30SP" -Android:text= "Show progress Diagram"/> -</LinearLayout>
Java Code Implementation Mainactivity.java:
1 Importandroid.app.Notification;2 ImportAndroid.app.NotificationManager;3 Importandroid.app.PendingIntent;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 Importandroid.graphics.BitmapFactory;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.os.Bundle;9 ImportAndroid.support.v7.app.NotificationCompat;Ten ImportAndroid.view.View; One Importandroid.widget.RemoteViews; A  Public classMainactivityextendsappcompatactivity { -     //define the notification useful ID -     Private Static Final intNo_3 =0x3; the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +     } -      Public  voidshow3 (View v) { +         FinalNotificationcompat.builder Builder =NewNotificationcompat.builder ( This); A Builder.setsmallicon (r.mipmap.huangyueying); atBuilder.setcontenttitle ("Download"); -Builder.setcontenttext ("Downloading"); -         FinalNotificationmanager Manager =(Notificationmanager) Getsystemservice (context.notification_service); - manager.notify (No_3, Builder.build ()); -Builder.setprogress (100,0,false); -         //Download and install threading simulations in         NewThread (NewRunnable () { - @Override to              Public voidrun () { +                  for(inti=0;i<100;i++){ -Builder.setprogress (100,i,false); the manager.notify (No_3,builder.build ()); *                     //Download Progress Tips $Builder.setcontenttext ("Download" +i+ "%");Panax Notoginseng                     Try { -Thread.Sleep (50);//Demo Hibernation 50 ms the}Catch(interruptedexception e) { + e.printstacktrace (); A                     } the                 } +                 //change the title and tips after the download is complete -Builder.setcontenttitle ("Start Installation"); $Builder.setcontenttext ("In-Installation ..."); $                 //Setup Progress is indeterminate for simulated installation -Builder.setprogress (0,0,true); - manager.notify (No_3,builder.build ()); the //Manager.cancel (no_3);//set the close notification bar -             }Wuyi }). Start (); the     } -}

We are just a simple simulation of the implementation of the results, in order to let everyone understand and skilled use of attribute methods, for the later physical projects to do technical reserves.

Related documents read:use notification in Android to implement the general notification bar (notification example one)use notification in Android to implement the general notification bar (notification example one)

Use notification to implement progress notification bar in Android (example three)

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.