Method Example of Android Programming to Realize the Effect of Notification Bar Progress Bar

  • 2021-08-21 21:24:59
  • OfStack

This paper describes the method of realizing the progress bar effect of notification bar by Android programming. Share it for your reference, as follows:


/**
 *  Notification management tool class 
 * 
 * @description : 
 * @author ldm
 * @date 2016-5-3  Morning 9:39:56
 */
public class NotificationUtil {
  private Context mContext;
  // NotificationManager  :   It is the management class of status bar notification, which is responsible for sending notification and clear notification. 
  private NotificationManager manager;
  //  Definition Map To save Notification Object 
  private Map<Integer, Notification> map = null;
  public NotificationUtil(Context context) {
    this.mContext = context;
    // NotificationManager  Yes 1 System Service , must pass  getSystemService() Method to get the. 
    manager = (NotificationManager) mContext
        .getSystemService(Context.NOTIFICATION_SERVICE);
    map = new HashMap<Integer, Notification>();
  }
  public void showNotification(int notificationId) {
    //  Judgment correspondence id Adj. Notification Has been displayed,   Avoid the same 1 A Notification Appear multiple times 
    if (!map.containsKey(notificationId)) {
      //  Create a notification object 
      Notification notification = new Notification();
      //  Set the notification bar to scroll text 
      notification.tickerText = " Start downloading xx Documents ";
      //  Set the display time 
      notification.when = System.currentTimeMillis();
      //  Set the icon for notification display 
      notification.icon = R.drawable.ic_launcher;
      //  Setting the characteristics of notifications :  When the notification is clicked, it disappears automatically 
      notification.flags = Notification.FLAG_AUTO_CANCEL;
      //  Set the operation of clicking on the notification bar 
      Intent in = new Intent(mContext, MainActivity.class);//  Click to jump to the specified page 
      PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, in,
          0);
      notification.contentIntent = pIntent;
      //  Set the display view of notifications 
      RemoteViews remoteViews = new RemoteViews(
          mContext.getPackageName(),
          R.layout.notification_contentview);
      //  Set the click event of the pause button 
      Intent pause = new Intent(mContext, MainActivity.class);//  Set to jump to the corresponding interface 
      PendingIntent pauseIn = PendingIntent.getActivity(mContext, 0, in,
          0);
      //  It can be passed here Bundle Equal transfer parameter 
      remoteViews.setOnClickPendingIntent(R.id.pause, pauseIn);
      /**********  Simple separation  **************************/
      //  Set the click event of the Cancel button 
      Intent stop = new Intent(mContext, MainActivity.class);//  Set to jump to the corresponding interface 
      PendingIntent stopIn = PendingIntent
          .getActivity(mContext, 0, in, 0);
      //  It can be passed here Bundle Equal transfer parameter 
      remoteViews.setOnClickPendingIntent(R.id.cancel, stopIn);
      //  Set the display view of notifications 
      notification.contentView = remoteViews;
      //  Issue a notice 
      manager.notify(notificationId, notification);
      map.put(notificationId, notification);//  Deposit Map Medium 
    }
  }
  /**
   *  Cancel notification action 
   * 
   * @description : 
   * @author ldm
   * @date 2016-5-3  Morning 9:32:47
   */
  public void cancel(int notificationId) {
    manager.cancel(notificationId);
    map.remove(notificationId);
  }
  public void updateProgress(int notificationId, int progress) {
    Notification notify = map.get(notificationId);
    if (null != notify) {
      //  Modify progress bar 
      notify.contentView.setProgressBar(R.id.pBar, 100, progress, false);
      manager.notify(notificationId, notify);
    }
  }
}

Layout file notification_contentview. xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Notification Bar Download Test " />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >
    <ProgressBar
      android:id="@+id/pBar"
      style="@android:style/Widget.ProgressBar.Horizontal"
      android:layout_width="match_parent"
      android:layout_height="4dp"
      android:layout_weight="1" />
    <Button
      android:id="@+id/pause"
      android:layout_width="match_parent"
      android:layout_height="30dp"
      android:layout_weight="2"
      android:text=" Suspend " />
    <Button
      android:id="@+id/cancel"
      android:layout_width="match_parent"
      android:layout_height="30dp"
      android:layout_weight="2"
      android:text=" Cancel " />
  </LinearLayout>
</LinearLayout>

Activity Simple test notification, project according to the need to use, such as file download to update the progress, cancel the corresponding operations.


/**
 * Notification Yes Android The specific status bar notification object in the project can be set icon Text, prompt sounds, vibrations, and so on. 
 *  Common properties: 
 * icon: Set the icon displayed on the notification 
 * tickerText: Set the text to scroll in the notification  
 * text: Set the contents of the notification 
 * flags: Setting the characteristics of notifications 
 * defaults: Set the default effect of notification 
 * when: Set the time when notifications are displayed 
 * contentView Set the content view for notification display 
 * sound: Set the sound of notification 
 * contentIntent: Set operations such as jumping when clicking notification 
 */
/**
 *  Realize the download progress bar style display in the notification bar Demo
 * 
 * @description : 
 * @author ldm
 * @date 2016-5-3  Morning 8:40:37
 */
public class MainActivity extends ActionBarActivity {
  private NotificationUtil mNotificationUtil;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNotificationUtil = new NotificationUtil(this);
    findViewById(R.id.send).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        mNotificationUtil.showNotification(100);//  Test notification 
      }
    });
  }
}

For Android notification function, please refer to the previous article https://www.ofstack.com/article/85807. htm

More readers interested in Android can check the topics on this site: "Introduction and Advanced Tutorial of Android Development", "Summary of Android Debugging Skills and Common Problem Solutions", "Summary of Android Basic Component Usage", "Summary of Android View View Skills", "Summary of Android Layout layout Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: