Android Notifications (1)

Source: Internet
Author: User

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)

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.