The Android project implements a system notification bar message with a progress bar _android

Source: Internet
Author: User
Tags stub

When we are doing Android development, we often encounter the background thread execution, such as downloading files, this time we want to let customers see the background has operations, then we can use the progress bar, so since running in the background, is to try not to occupy the current operating space, The best way for users to do other things is to have a notification message in the notification bar and a progress bar. This article gives an example to the reader for reference.
The effect chart is as follows:

The main interface has only one button and it's not on file.

The notification bar shows the layout files used Content_view.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" 
  Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" "#00000000" android:orientation= "vertical" android:padding= "5DP" > <imageview android:id= "@+id/content_view_image" 
  "Android:layout_width=" 25DP "android:layout_height=" 25DP "android:src=" @drawable/logo "/> <textview android:id= "@+id/content_view_text1" android:layout_width= "Wrap_content" Android:layout_heigh t= "Wrap_content" android:text= "0%" android:textcolor= "#000000" android:layout_torightof= "@id/content_view_i Mage "Android:layout_centerhorizontal=" true "android:layout_margintop=" 5DP "android:layout_marginleft=" 15DP 
    "/> <progressbar android:id= @+id/content_view_progress" android:layout_width= "Fill_parent" Android:layout_height= "Wrap_coNtent "style=" @android: Style/widget.progressbar.horizontal "android:max=" "android:layout_below=" @id/con 
 Tent_view_image "android:layout_margintop=" 4DP "/> </RelativeLayout>

Master Run class:

Package yyy.testandroid4; 
Import Java.util.Timer; 
 
Import Java.util.TimerTask; 
Import android.app.Activity; 
Import Android.app.AlertDialog.Builder; 
Import android.app.Notification; 
Import Android.app.NotificationManager; 
Import android.app.PendingIntent; 
Import Android.content.DialogInterface; 
Import android.content.Intent; 
Import android.content.pm.PackageManager.NameNotFoundException; 
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 Testandroid4activity extends activity {private Handler Handler = new Handler () {@Override 
      public void Handlemessage (message msg) {//TODO auto-generated Method Stub super.handlemessage (msg); Switch (msg.what) {case 0:notif.contentview.settextviewtext (R.id.content_view_text1, len+ "%"); 
        Notif.contentView.setProgressBar (R.id.content_view_progress, Len, false); 
         
        Manager.notify (0, Notif); 
      Break 
        Case 1:toast.maketext (testandroid4activity.this, "Download Complete", 0). Show (); 
      Break 
      Default:break; 
   
  } 
    } 
     
  }; 
  Private Button Update,cancel; 
  private int localversion,serverversion; 
  private int len; 
  Private Notificationmanager Manager; 
  Private Notification Notif; /** called the activity is a. 
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.main); 
    
    Update = (Button) Findviewbyid (r.id.update); Update.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View arg0) {//TOD O auto-generated Method Stub//click on the notification bar to open the activity Intent Intent = new Intent (testandroid4activity.thIs,otheractivity.class); 
           Pendingintent pintent = pendingintent.getactivity (testandroid4activity.this, 0, intent, 0); 
        Manager = (Notificationmanager) getsystemservice (Notification_service); 
        Notif = new Notification (); 
        Notif.icon = R.drawable.logo; 
        Notif.tickertext = "new Notice"; 
        The notification bar displays the layout files used Notif.contentview = new Remoteviews (Getpackagename (), R.layout.content_view); 
        Notif.contentintent = pintent; 
        Manager.notify (0, Notif); 
      New Downloadthread (). Start (); 
     
     
     
  } 
    }); 
    } private class Downloadthread extends thread{private timer timer = new timer (); 
      @Override public void Run () {//TODO auto-generated Method Stub Super.run (); Timer.schedule (New TimerTask () {@Override public void run () {//TODO auto-generated method 
          Stub message msg = new Message (); Msg.what = 0; 
          Msg.obj = Len; 
           
          Handler.sendmessage (msg); 
            if (len = =) {Timer.cancel (); 
          Handler.sendemptymessage (1); 
      }}, 0, 1000); 
      len = 0; 
          try {while (Len < MB) {len++; 
        Thread.Sleep (1000); 
      The catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); 
 } 
    } 
     
  } 
   
   
}

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.