Statement:
// Notification Manager Private Notificationmanager Nmanager;
Dominant constructor private notification.builder builder;
Notification initialization
/** * Notification initialization * /private void GetNotification () {
Notification Manager Nmanager = (notificationmanager) getsystemservice (
Context.notification_service); /System Notifications
//Set the title and content of the notification through the constructor Builder = new Notification.builder (Getapplicationcontext ()); Builder.setsmallicon (r.drawable.ic_launcher); Builder.setticker ("Notifications come up");//Hint title builder.setcontenttitle ("Download"); Notification Content title Builder.setcontenttext ("Downloading .....") ");//notification content }
Notification Progress Update
/*** Update UI*/ PrivateHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {//Receive line Cheng messages sent//Update UIBuilder.setprogress (MSG.ARG1,false);
1001 is the unique number of the notification and can be called Nmanager.cancel (1001); Unregister this notification nmanager.notify (1001, Builder.build ()); if(Msg.what = = 1){ //Download CompleteString name = Uri.split ("/") [-1]; Builder.setcontenttitle (name); Builder.setcontenttext ("Download Complete"); Toast.maketext (Getapplicationcontext (),"Download Complete", 1). Show (); } } };
/*** Download and execute in thread, **/ Public classMyrunnableImplementsRunnable {@Override Public voidrun () {//data stored for reading byte[] result =NULL; HttpClient Client=Newdefaulthttpclient (); Try{log.i ("Tag", "uri>>" +URI); HttpGet Get=NewHttpGet (URI); HttpResponse Response=Client.execute (GET); Httpentity Entity=response.getentity (); intStatecode =response.getstatusline (). Getstatuscode (); if(Statecode! = 200) {log.i ("Tag", statecode+ "-Download failed--"); } //get the network input streamInputStream in =entity.getcontent (); //get total network data length LongTotal =entity.getcontentlength (); byte[] bs =New byte[256]; intLen_per = 0;//the length of each read,Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); intReadedlen = 0;//The amount of data that has been read while((Len_per = In.read (BS))! =-1) {baos.write (BS,0, Len_per); Readedlen+=Len_per; intProgress = (int) ((readedlen*100)/(float) total); Message msg=Message.obtain (); Msg.arg1=progress; Handler.sendmessage (msg); } result=Baos.tobytearray (); Handler.sendemptymessage (1); }Catch(Exception e) {e.printstacktrace (); }finally { if(Client! =NULL) {Client.getconnectionmanager (). Shutdown (); } } } }
Android Notifications (1)