自定义Notification并利用Handler更新Notification

简介:
 

在消息通知的时候,我们经常用到两个控件Notification和Toast。特别是重要的和需要长时间显示的信息,用Notification最合适不过了。他可以在顶部显示一个图标以标示有了新的通知,当我们拉下通知栏的时候,可以看到详细的通知内容。

最典型的应用就是未看短信和未接来电的显示,还有QQ企鹅,我们一看就知道有一个未接来电或者未看短信,正在挂QQ。同样,我们也可以自定义一个Notification来定义我们自己的程序想要传达的信息。

Notification我把他分为两种,一种是默认的显示方式,另一种是自定义的。

默认的Notification设置方式见我的早一篇博文
http://www.liuzhaocn.com//?p=364

在这里我主要介绍自定义Notification的用法,自定义Notification原理很简单,就是把通知区域自己写一个View,在代码里把Notification的contantview设置为我们自定义的view即可。

例如,这里有一个View

XML语言:   notification.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout   android:id= “@+id/noti_lay”
xmlns:android= “http://schemas.android.com/apk/res/android”
android:layout_width= “wrap_content”
android:layout_height= “fill_parent”
android:orientation= “vertical”
>
      <LinearLayout   android:id= “@+id/top_lay”
          xmlns:android= “http://schemas.android.com/apk/res/android”
          android:layout_width= “wrap_content”
          android:layout_height= “wrap_content”
          android:orientation= “horizontal”
          >
          <ImageView   android:id= “@+id/down_iv”
              android:layout_width= “30sp”  �
              android:layout_height= “30sp”
              android:src= “@drawable/downnoti”
              />
          <TextView   android:id= “@+id/down_tv”
              android:layout_width= “wrap_content”  �
              android:layout_height= “fill_parent”
              android:textSize= “20sp”
              android:textColor= “#000000″
              />
      </LinearLayout>
      <LinearLayout   android:id= “@+id/progress_lay”
          xmlns:android= “http://schemas.android.com/apk/res/android”
          android:layout_width= “wrap_content”
          android:layout_height= “fill_parent”
          android:orientation= “horizontal”
          >
          <TextView   android:id= “@+id/down_rate”
              android:layout_width= “wrap_content”  �
              android:layout_height= “fill_parent”
              android:textSize= “20sp”
              android:textColor= “#000000″
              />
          <ProgressBar   android:id= “@+id/pb”
              android:layout_width= “260dip”
              android:layout_height= “wrap_content”
              style= “?android:attr/progressBarStyleHorizontal”
              android:layout_gravity= “center_vertical” />
      </LinearLayout>
</LinearLayout>

把他放在layout文件夹下。
这就是在通知区域要显示的东西,显示效果如下:

操作Notification的代码:

Java语言:   Codee#17015
public   Notification   downloadNotification;
public   NotificationManager   downloadNM;

//……..

/**
                 * 添加顶部下载通知
                 * liuzhao
                 */
                downloadNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                downloadNotification = new Notification(R.drawable.downnoti,apkname+“下载…”,System.currentTimeMillis());
                downloadNotification.contentView = newRemoteViews(getPackageName(),R.layout.notification);
                //设置进度条的最大进度和初始进度
                downloadNotification.contentView.setProgressBar(R.id.pb, 100,0, false);
                //显示下载的包名
                downloadNotification.contentView.setTextViewText(R.id.down_tv, apkname);
                //显示下载的进度
                downloadNotification.contentView.setTextViewText(R.id.down_rate, “0%”);
              �
                Intent notificationIntent = new Intent(tykmAndroid.this,tykmAndroid.class);
                PendingIntent contentIntent =PendingIntent.getActivity(tykmAndroid.this,0,notificationIntent,0);
                downloadNotification.contentIntent = contentIntent;
                //显示通知
                downloadNM.notify(downNotiID, downloadNotification);
              �
                //…….

以上是让通知显示出来,下边要根据下载的进度,改变进度条的进度:
下载进程中的核心代码:

Java语言:   下载进程

InputStream   is   =   conn . getInputStream();
                              FileOutputStream   fos   =   new   FileOutputStream( file);
                              byte []   bt   =   new   byte [ 1024 ];
                              int   i   =   0;
                              int   progresCount   =   0;
                              int   times   =   0;
                              while  (( i   =   is . read( bt))   >   0)   {
                                  fos . write( bt ,   0 ,   i);
                                  tykmAndroid . flagloading = 1;
                                  firstCat . flagloading = 1;
                                  long   tempFileLength   =   file . length();
                                  if(( times   ==   512)||( tempFileLength   ==   length )){
                                      //更新通知中的进度条
                                      int   progress   =  ( int)( tempFileLength * 100 / length);
                                      if( progresCount < progress ){
                                         
                                          progresCount = progress;
                                          System . out . println( "progresCount=" + progresCount);
                                         
                                          //利用Handler在主线程中更新
                                          Message   msg   =   new   Message();
                                          msg . what   =   6001;
                                          Bundle  b   =   new   Bundle();
                                        b . putInt( "progressValue" ,   progresCount);
                                        b . putString( "apkName" ,   filename);
                                          msg . setData(b);
                                          tykmAndroid . tykmAndroid_Entity . handler . sendMessage( msg);
                                         
                                      }
                                      times = 0;
                                      continue;
                                  }
                                  times ++;
                              }
                              fos . flush();
                              fos . close();
                              is . close();
                              conn . disconnect();

这里没有直接对通知进行更新,而是用了一个handler,让UI线程去更新。
UI线程中的Handler是这样写的:

Java语言:   Handler中的核心代码

case   6001 : //下载的时候更新Notification
                  Bundle  b   =   msg . getData();
                  //得到进度条的当前值
                  int   progressCount   =  b . getInt( "progressValue");
                  //得到包名
                  String   apkName   =  b . getString( "apkName");
                 
                  //下载未完成,显示进度条
                  if( progressCount   != 100   ){
                      //设置进度条
                      downloadNotification . contentView . setProgressBar( R . id .pb ,   100 ,   progressCount ,   false);
                      //设置百分比
                      downloadNotification . contentView . setTextViewText( R . id . down_rate , "" + progressCount + "%");
                      //更新进度条
                  }
                  //下载完成,显示下载完成
                  else {
                      downloadNotification . contentView . removeAllViews( R . id . progress_lay);
                      PendingIntent   contentIntent   = PendingIntent . getActivity( tykmAndroid . tykmAndroid_Entity ,   0 ,   getIntent (),   0);  
                    downloadNotification . setLatestEventInfo( tykmAndroid . tykmAndroid_Entity , apkName , "下载完成" , contentIntent);
                  }
                  downloadNM . notify( downNotiID ,   downloadNotification);
                  break;

这里有一段代码:
在Download线程里面:
if((times == 512)||(tempFileLength == length)){
这是防止频繁地更新通知,而导致系统变慢甚至崩溃。
非常重要。。。。。

相关文章
|
前端开发 安全 数据安全/隐私保护
支付宝支付流程解读
支付宝支付流程解读
|
10月前
|
敏捷开发 监控 数据可视化
关键链项目管理:全球化与本地化的完美结合
关键链项目管理(CCPM)由以色列物理学家艾利·高德拉特提出,基于“约束理论”,通过优化资源分配、引入缓冲管理和动态调整,显著提升项目效率和成功率。本文探讨了CCPM的国际化实践现状、应用优势及未来发展趋势,特别是在欧美、亚太及新兴市场的广泛应用。CCPM在缩短项目周期、提高资源利用率、降低风险和增强团队协作方面表现出色。未来,CCPM将与数字化工具深度融合,并在全球化与本地化结合中进一步普及,成为复杂项目管理的核心方法。
|
Swift iOS开发
iOS Swift使用Alamofire请求本地服务器报错-1002
iOS Swift使用Alamofire请求本地服务器报错-1002
283 1
|
JavaScript Java 测试技术
基于SpringBoot+Vue的城市公交调度系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的城市公交调度系统的详细设计和实现(源码+lw+部署文档+讲解等)
167 5
大尺度信道建模 | 带你读《大规模天线波束赋形技术原理与设计 》之二十四
本节将介绍 3D 信道模型中 不同传输场景的大尺度衰落模型,包括路损计算、穿透损耗、直射径概率、阴 影衰落等。
大尺度信道建模  | 带你读《大规模天线波束赋形技术原理与设计 》之二十四
|
机器学习/深度学习 数据采集 人工智能
构建一个基于AI的文本分类系统
【6月更文挑战第1天】构建基于AI的文本分类系统涉及数据预处理、特征提取、模型训练和预测。步骤包括:收集标注文本,进行预处理(清洗、分词、去停用词);使用词袋模型、TF-IDF或词嵌入提取特征;选择并训练模型(如朴素贝叶斯、SVM、CNN等);评估优化模型性能;最后部署模型进行预测,并定期更新维护。未来,关注点将扩展至模型的鲁棒性、可解释性和安全性。
|
Kubernetes API Python
|
XML Java 开发者
Spring框架: 什么是依赖注入(Dependency Injection)?
Spring框架: 什么是依赖注入(Dependency Injection)?
432 2
|
Java API 开发工具
Apk转Aab(Android App Bundle)
Apk转Aab(Android App Bundle)
1429 0