Android adds a progress bar for notification

Source: Internet
Author: User
Package com. Notification; import Android. App. activity;
Import Android. App. notification;
Import Android. App. icationicationmanager;
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 = NULL;
Private icationicationmanager manager = NULL; Public handler;
Private int _ progress = 0;
Private thread = NULL;
Private Boolean isstop = false; // when the interface is processing the stopped status, it is set to cancel the progress bar.
@ Override
Protected void onpause (){
// Todo auto-generated method stub
Isstop = false;
Manager. Cancel (icationication_id); Super. onpause ();
}/** Called when the activity is first 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, "Incoming notification", System
. Currenttimemillis ());
Notification. Icon = R. drawable. Icon; // you can call remoteviews to set the view attributes in notification.
Notification. contentview = new remoteviews (getapplication ()
. Getpackagename (), R. layout. custom_dialog );
Notification. contentview. setprogressbar (R. Id. petabytes, 100, 0, false );
Notification. contentview. settextviewtext (R. Id. TV, "progress" + _ progress
+ "% ");
// Use pendingintetn
// Set the activity to jump to. You can also set to send a service or broadcast here,
// However, all operations here must be triggered after the user clicks notification.
Notification. contentintent = pendingintent. getactivity (this, 0,
New intent (this, remoteview. Class), 0 );
// Obtain a notificationmanger object, which can be used to manage the notification in a unified manner. You only need to know the ID
Manager = (icationicationmanager) getsystemservice (notification_service) ;}@ override
Public void onclick (view v ){
// Todo auto-generated method stub
Isstop = true;
Manager. icationy (icationication_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 (Message MSG ){
// Todo auto-generated method stub
Notification. contentview. setprogressbar (R. Id. PBS, 100, MSG. arg1,
False );
Notification. contentview. settextviewtext (R. Id. TV, "progress"
+ Msg. arg1 + "% ");
Manager. Y (icationication_id, notification); If (msg. arg1 = 100 ){
_ Progress = 0;
Manager. Cancel (icationication_id );
Isstop = false;
Toast. maketext (nofificationactivity. This, "download completed", 1000)
. Show ();
}
Super. handlemessage (MSG );
}
};
}
}

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.