Android实现为Notification加上一个进度条的方法

 更新时间:2016年10月24日 15:15:00   作者:老码农豆豆  
这篇文章主要介绍了Android实现为Notification加上一个进度条的方法,结合实例形式分析了Android针对Notification组件的相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android实现为Notification加上一个进度条的方法。分享给大家供大家参考,具体如下:

package com.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
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 nofificationActivity extends Activity implements OnClickListener {
  private static final int NOTIFICATION_ID = 0x12;
  private Notification notification = null;
  private NotificationManager manager = null;
  public Handler handler;
  private int _progress = 0;
  private Thread thread = null;
  private boolean isStop = false;
  // 当界面处理停止的状态 时,设置让进度条取消
  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    isStop = false;
    manager.cancel(NOTIFICATION_ID);
    super.onPause();
  }
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(this);
    notification = new Notification(R.drawable.icon, "带进条的提醒", System
        .currentTimeMillis());
    notification.icon = R.drawable.icon;
    // 通过RemoteViews 设置notification中View 的属性
    notification.contentView = new RemoteViews(getApplication()
        .getPackageName(), R.layout.custom_dialog);
    notification.contentView.setProgressBar(R.id.pb, 100, 0, false);
    notification.contentView.setTextViewText(R.id.tv, "进度" + _progress
        + "%");
    // 通过PendingIntetn
    // 设置要跳往的Activity,这里也可以设置发送一个服务或者广播,
    // 不过在这里的操作都必须是用户点击notification之后才触发的
    notification.contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, remoteView.class), 0);
    // 获得一个NotificationManger 对象,此对象可以对notification做统一管理,只需要知道ID
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  }
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    isStop = true;
    manager.notify(NOTIFICATION_ID, notification);
    thread = new Thread(new Runnable() {
      @Override
      public void run() {
        Thread.currentThread();
        // TODO Auto-generated method stub
        while (isStop) {
          _progress += 10;
          Message msg = handler.obtainMessage();
          msg.arg1 = _progress;
          msg.sendToTarget();
          try {
            Thread.sleep(500);
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    });
    thread.start();
    handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        notification.contentView.setProgressBar(R.id.pb, 100, msg.arg1,
            false);
        notification.contentView.setTextViewText(R.id.tv, "进度"
            + msg.arg1 + "%");
        manager.notify(NOTIFICATION_ID, notification);
        if (msg.arg1 == 100) {
          _progress = 0;
          manager.cancel(NOTIFICATION_ID);
          isStop = false;
          Toast.makeText(nofificationActivity.this, "下载完毕", 1000)
              .show();
        }
        super.handleMessage(msg);
      }
    };
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android视图View技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android Application的使用全面解析

    Android Application的使用全面解析

    这篇文章主要为大家介绍了Android Application的使用全面解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android实现登陆界面的记住密码功能

    Android实现登陆界面的记住密码功能

    这篇文章主要为大家详细介绍了Android实现登陆界面的记住密码功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 自定义Android六边形进度条(附源码)

    自定义Android六边形进度条(附源码)

    这篇文章主要介绍了自定义Android六边形进度条,本文设计的进度条是六边形的,对进度条感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Android Map新用法:MapFragment应用介绍

    Android Map新用法:MapFragment应用介绍

    MapView ,MapActivity 这种的局限在于,必须要继承MapActivity,否则无法使用MapView,但是,MapFragment 这种的局限在于,必须要安装Google Play Service ,也就是说必须是原生rom。而且sdk要在12以上
    2013-01-01
  • android文件管理器用法详解

    android文件管理器用法详解

    这篇文章主要为大家详细介绍了android文件管理器的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Android开发vsts agent支持自定义task过程详解

    Android开发vsts agent支持自定义task过程详解

    这篇文章主要介绍了Android开发vsts agent支持自定义task过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • 如何设置Android studio 3.0显示光标返回上一次浏览位置的箭头图标

    如何设置Android studio 3.0显示光标返回上一次浏览位置的箭头图标

    这篇文章主要介绍了如何设置Android studio 3.0显示光标返回上一次浏览位置的箭头图标 很多朋友反映刚升级了Android studio 3.0,发现光标返回上一次浏览位置的箭头图标没有了,下文给大家介绍的非常详细,需要的朋友可以参考下
    2017-11-11
  • Android富文本实现的几种方式汇总

    Android富文本实现的几种方式汇总

    由于项目中需要使用到富文本显示和编辑,索性整理下,这篇文章主要给大家介绍了关于Android富文本实现的几种方式,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • Recycleview实现无限自动轮播

    Recycleview实现无限自动轮播

    这篇文章主要为大家详细介绍了Recycleview实现无限自动轮播,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • Android Studio做超好玩的拼图游戏 附送详细注释源码

    Android Studio做超好玩的拼图游戏 附送详细注释源码

    这篇文章主要介绍了用Android Studio做的一个超好玩的拼图游戏,你是0基础Android小白也能包你学会,另外附送超详细注释的源码,建议收藏!
    2021-08-08

最新评论