Android implements the method of adding a progress bar to the notification _android

Source: Internet
Author: User
Tags sqlite database stub

The example in this article describes the approach that Android implements to add a progress bar to notification. Share to everyone for your reference, specific as follows:

Package com.notification;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.RemoteViews;
Import Android.widget.Toast;  public class Nofificationactivity extends activity implements Onclicklistener {private static final int notification_id
  = 0x12;
  Private Notification Notification = null;
  Private Notificationmanager manager = null;
  Public Handler Handler;
  private int _progress = 0;
  Private thread thread = NULL;
  Private Boolean isstop = false; When the interface processes a stopped state, set the progress bar to cancel @Override protected void OnPause () {//TODO auto-generated method Stub isstop = Fals
    E
    Manager.cancel (notification_id);
  Super.onpause (); }/** called when the The created.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Button btn = (button) Findviewbyid (R.ID.BUTTON01);
    Btn.setonclicklistener (this);
    notification = new Notification (R.drawable.icon, "Reminder with Bar", System. Currenttimemillis ());
    Notification.icon = R.drawable.icon; Set the properties of view in notification by remoteviews Notification.contentview = new Remoteviews (Getapplication (). Getpacka
    Gename (), r.layout.custom_dialog);
    Notification.contentView.setProgressBar (R.ID.PB, 0, false);
    Notification.contentView.setTextViewText (R.id.tv, "progress" + _progress + "%"); by PENDINGINTETN//Setting the activity to be skipped, you can also set up to send a service or broadcast,//But the operation here must be triggered only after the user clicks on the notification Notification.con
    Tentintent = pendingintent.getactivity (this, 0, new Intent (this, remoteview.class), 0); Get a Notificationmanger object that can be managed uniformly for notification, just know the ID manAger = (Notificationmanager) getsystemservice (Notification_service);
    @Override public void OnClick (View v) {//TODO auto-generated method Stub isstop = true;
    Manager.notify (notification_id, NOTIFICATION);
        thread = new Thread (new Runnable () {@Override public void run () {thread.currentthread ();
          TODO auto-generated Method Stub while (isstop) {_progress = 10;
          Message msg = Handler.obtainmessage ();
          MSG.ARG1 = _progress;
          Msg.sendtotarget ();
          try {thread.sleep (500);
          catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
    }
        }
      }
    });
    Thread.Start (); Handler = new Handler () {@Override public void Handlemessage (msg) {//TODO auto-generated Me Thod stub Notification.contentView.setProgressBar (R.ID.PB, MSG.ARG1, FALSE);
        Notification.contentView.setTextViewText (R.id.tv, "progress" + MSG.ARG1 + "%");
        Manager.notify (notification_id, NOTIFICATION);
          if (Msg.arg1 =) {_progress = 0;
          Manager.cancel (notification_id);
          Isstop = false;
        Toast.maketext (nofificationactivity.this, "Download Complete", 1000). Show ();
      Super.handlemessage (msg);
  }
    };

 }
}

For more information on Android-related content readers can view the site topics: "The basic components of Android Usage Summary", "Android View Overview", "Android Resource Operation tips Summary", "Android File Operation skills Summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", A summary of the operational skills of Android programming and the summary of the usage of Android controls

I hope this article will help you with the Android program.

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.