Detailed Android use notification implementation Progress notification bar (example three) _android

Source: Internet
Author: User

We are in the process of using the app, the software will occasionally prompt us to update the version, we click to confirm the update, we will be in the Notification column download update progress (known length of the progress bar) and the installation (the progress bar), which is what we want to implement today. The implementation effect is as follows:

Before the code implements functionality, let's 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). The notification is then issued. Then, as the operation progresses, increment the progress value, and then update the notification. At the end of the operation, the progress value should equal the maximum value. The usual way is to call Setprogress () to set the maximum value of 100, and then to increase the percentage of progress completed. You can display the progress bar when the operation is complete, or you can remove it. In this case, remember to update the notification text, the display 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, indeterminate is indeterminate (set to True, then indeterminate, and vice versa)

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

To be able to use an indeterminate activity indicator, use the setprogress (0, 0, True) method to add (the first two parameters are ignored) and then post the notification. Unless you specify the animation effect, the indicator is the same style.

Release the notification at the start of the operation, and the animation will continue until you change 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 the operation to complete, 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 start implementing the code:

Layout Click on the trigger button layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  xmlns:tools=" Http://schemas.android.com/tools "
  android:id=" @+id/activity_content
  " Android:layout_width= "Match_parent"
  android:layout_height= "match_parent"
  tools:context= " Com.example.administrator.day12.ContentActivity ">
  <textview
    android:layout_width=" Match_parent "
    android:layout_height= "match_parent"
    android:gravity= "center"
    android:textsize= "30SP"
    Android:text= "Show Progress Real map"/>
</LinearLayout>

Java Code Implementation Mainactivity.java:

Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.content.Context;
Import android.content.Intent;
Import Android.graphics.BitmapFactory;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.support.v7.app.NotificationCompat;
Import Android.view.View;
Import Android.widget.RemoteViews;
  public class Mainactivity extends appcompatactivity {//definition notification practical ID private static final int no_3 =0x3;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
    public void show3 (View v) {final Notificationcompat.builder Builder = new Notificationcompat.builder (this);
    Builder.setsmallicon (r.mipmap.huangyueying);
    Builder.setcontenttitle ("Download");
    Builder.setcontenttext ("Downloading"); Final Notificationmanager manager = (Notificationmanager) getsystemservice (context.notification_service);
    Manager.notify (No_3, Builder.build ());
    Builder.setprogress (100,0,false);
          Download and install thread emulation new thread (new Runnable () {@Override public void run () {for (int i=0;i<100;i++) {
          Builder.setprogress (100,i,false);
          Manager.notify (No_3,builder.build ());
          Download progress prompts Builder.setcontenttext ("Download" +i+ "%"); try {thread.sleep (50);//Demo Hibernate 50 ms} catch (Interruptedexception e) {e.printstacktrace ()
          ;
        After the download is complete, change the caption and prompt for information builder.setcontenttitle ("Start Installation");
        Builder.setcontenttext ("Install ...");
        Set the schedule to be indeterminate, used to simulate the installation of builder.setprogress (0,0,true);
Manager.notify (No_3,builder.build ());
  Manager.cancel (No_3),//Set close notification Bar}}). Start ();

 }
}

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.