温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android编程如何实现通知栏进度条效果

发布时间:2021-06-28 12:52:25 来源:亿速云 阅读:272 作者:小新 栏目:移动开发

这篇文章主要介绍了Android编程如何实现通知栏进度条效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

具体如下:

public class NotificationUtil {
  private Context mContext;
  // NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。
  private NotificationManager manager;
  // 定义Map来保存Notification对象
  private Map<Integer, Notification> map = null;
  public NotificationUtil(Context context) {
    this.mContext = context;
    // NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。
    manager = (NotificationManager) mContext
        .getSystemService(Context.NOTIFICATION_SERVICE);
    map = new HashMap<Integer, Notification>();
  }
  public void showNotification(int notificationId) {
    // 判断对应id的Notification是否已经显示, 以免同一个Notification出现多次
    if (!map.containsKey(notificationId)) {
      // 创建通知对象
      Notification notification = new Notification();
      // 设置通知栏滚动显示文字
      notification.tickerText = "开始下载xx文件";
      // 设置显示时间
      notification.when = System.currentTimeMillis();
      // 设置通知显示的图标
      notification.icon = R.drawable.ic_launcher;
      // 设置通知的特性: 通知被点击后,自动消失
      notification.flags = Notification.FLAG_AUTO_CANCEL;
      // 设置点击通知栏操作
      Intent in = new Intent(mContext, MainActivity.class);// 点击跳转到指定页面
      PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, in,
          0);
      notification.contentIntent = pIntent;
      // 设置通知的显示视图
      RemoteViews remoteViews = new RemoteViews(
          mContext.getPackageName(),
          R.layout.notification_contentview);
      // 设置暂停按钮的点击事件
      Intent pause = new Intent(mContext, MainActivity.class);// 设置跳转到对应界面
      PendingIntent pauseIn = PendingIntent.getActivity(mContext, 0, in,
          0);
      // 这里可以通过Bundle等传递参数
      remoteViews.setOnClickPendingIntent(R.id.pause, pauseIn);
      /********** 简单分隔 **************************/
      // 设置取消按钮的点击事件
      Intent stop = new Intent(mContext, MainActivity.class);// 设置跳转到对应界面
      PendingIntent stopIn = PendingIntent
          .getActivity(mContext, 0, in, 0);
      // 这里可以通过Bundle等传递参数
      remoteViews.setOnClickPendingIntent(R.id.cancel, stopIn);
      // 设置通知的显示视图
      notification.contentView = remoteViews;
      // 发出通知
      manager.notify(notificationId, notification);
      map.put(notificationId, notification);// 存入Map中
    }
  }
  /**
   * 取消通知操作
   * 
   * @description:
   * @author ldm
   * @date 2016-5-3 上午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) {
      // 修改进度条
      notify.contentView.setProgressBar(R.id.pBar, 100, progress, false);
      manager.notify(notificationId, notify);
    }
  }
}

布局文件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="通知栏下载测试" />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >
    <ProgressBar
      android:id="@+id/pBar"
      
      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="暂停" />
    <Button
      android:id="@+id/cancel"
      android:layout_width="match_parent"
      android:layout_height="30dp"
      android:layout_weight="2"
      android:text="取消" />
  </LinearLayout>
</LinearLayout>

Activity中简单测试发通知,项目中根据需要使用,比如文件下载中要更新进度,取消时进行对应操作等。

/**
 * Notification是Android项目中具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。
 * 常用属性:
 * icon:设置通知上显示的图标
 * tickerText:设置通知中滚动显示的文字 
 * text:设置通知的内容
 * flags:设置通知的特性
 * defaults:设置通知默认效果
 * when:设置通知显示的时间
 * contentView:设置通知显示的内容视图
 * sound:设置通知的声音
 * contentIntent:设置点击通知时的跳转等操作
 */
/**
 * 在通知栏中实现下载进度条样式展示Demo
 * 
 * @description:
 * @author ldm
 * @date 2016-5-3 上午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);// 测试发出通知
      }
    });
  }
}

感谢你能够认真阅读完这篇文章,希望小编分享的“Android编程如何实现通知栏进度条效果”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI