Android倚天剑之Notification之动感地带

转摘自:http://blog.csdn.net/leverage_1229/article/details/9060555


上文我们介绍怎样管理和删除通知,以及怎样实现保存用户预期导航体验的通知。本文作为Android平台Notification的最终章,我们将会给通知融入更多DIY的元素,大胆地在这把“倚天剑”上烙下自己的印记^-^。在此之前,先来看下如何在通知中显示一个进度条。

一、显示进度的通知

        通知可以包括一个动画进度指示器以显示用户正在运行的操作的进度状态。如果你能估计这种操作需要花费多长时间,可以使用“determinate”形式的指示器(一个progress bar)。如果你不能估计花费的时间,那就使用“indeterminate”形式的指示器。

1.显示一个固定的时间进度指示器

(1).技术要点

调用setProgress()方法添加进度指示器到你的通知中。

(2).代码陈列

[java]  view plain copy print ?
  1. final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)  
  2.             .setContentInfo(String.valueOf(++progressNum))  
  3.             .setContentTitle("Picture Download")  
  4.             .setContentText("Download in progress")  
  5.             .setDefaults(Notification.DEFAULT_ALL)  
  6.             .setLargeIcon(icon)  
  7.             .setSmallIcon(R.drawable.stat_notify_gmail)  
  8.             .setTicker("Progress Notification")  
  9.             .setOngoing(true);  
  10.         // Start a lengthy operation in a background thread  
  11.         new Thread(new Runnable() {  
  12.             @Override  
  13.             public void run() {  
  14.                 int incr;  
  15.                 // Do the "lengthy" operation 20 times  
  16.                 for (incr = 0; incr <= 100; incr+=5) {  
  17.                     builder.setProgress(100, incr, false);  
  18.                     mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());  
  19.                       
  20.                     try {  
  21.                         Thread.sleep(1000);  
  22.                     } catch (InterruptedException e) {  
  23.                         Log.d(TAG, "sleep failure");  
  24.                     }  
  25.                 }  
  26.                 builder.setContentText("Download complete")  
  27.                     .setProgress(00false)  
  28.                     .setOngoing(false);  
  29.                 mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());  
  30.             }  
  31.         }).start();  
(3).效果展示

2.显示一个持续的Activity指示器

(1).技术要点

调用setProgress(0, 0, true)添加Activity指示器到你的通知中,前面两个参数可以忽略。

(2).代码陈列

[java]  view plain copy print ?
  1. builder.setProgress(100, incr, false);  
  2. mNotiMgr.notify(0, mBuilder.build());  

(3).效果展示

二、自定义样式的通知

        通知框架允许你自定义通知布局,它在一个RemoteViews对象中定义了通知的布局。自定义布局通知和正常的通知类似,它们都是基于一个RemoteViews定义在一个XML布局文件中。自定义通知的可用高度取决于通知view的布局。正常view布局限制为64dp,展开view布局限制为256dp。自定义通知布局,通过实例化一个RemoteViews对象然后inflates一个xml布局文件启动。不再调用setContentTitle()方法,而使用setContent()方法来设置自定义通知的内容细节。使用这个方法在RemoteViews中来设置view子类的值。

1.技术要点

(1).为通知创建一个单独的xml布局文件。

(2).在你的应用程序中,使用RemoteViews方法来定义你通知的icon和文本。调用setContent()方法put这个RemoteViews对象到你的NotificationCompat.Builder中。避免正在RemoteViews对象中设置Drawable背景,因为你的文本颜色可能会变的看不清。

2.代码陈列

工程包目录

自定义样式通知创建和发布方法:showCustomNoti()

[java]  view plain copy print ?
  1. /** 
  2.  * 自定义样式通知 
  3.  */  
  4. private void showCustomNoti() {  
  5.     RemoteViews views = new RemoteViews(getPackageName(), R.layout.custom);  
  6.     Intent intent = new Intent(INTENT_ACTION);  
  7.     intent.putExtra("isPlay"true);  
  8.       
  9.     PendingIntent pendingIntent = PendingIntent.getBroadcast(this0, intent, 0);  
  10.     views.setOnClickPendingIntent(R.id.play_pause_music, pendingIntent);  
  11.     NotificationCompat.Builder builder = new NotificationCompat.Builder(this)  
  12.         .setContent(views)  
  13.         .setDefaults(Notification.DEFAULT_ALL)  
  14.         .setLargeIcon(icon)  
  15.         .setSmallIcon(R.drawable.music_icon)  
  16.         .setTicker("Custom Notification")  
  17.         .setOngoing(true);  
  18.     mNotiMgr.notify(CUSTOM_NOTI_ID, builder.build());  
  19. }  

自定义通知布局文件:custom.xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center_vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/songer"  
  9.         android:layout_width="64dp"  
  10.         android:layout_height="64dp"  
  11.         android:src="@drawable/songer" />  
  12.       
  13.     <LinearLayout  
  14.         android:layout_width="0dp"  
  15.         android:layout_height="match_parent"  
  16.         android:gravity="center_vertical"  
  17.         android:orientation="vertical"  
  18.         android:layout_weight="1">  
  19.           
  20.         <TextView  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="@string/song_name"  
  24.             android:textSize="18sp"  
  25.             android:gravity="center_horizontal" />  
  26.           
  27.         <TextView  
  28.             android:layout_width="match_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="@string/songer_name"  
  31.             android:textSize="14sp"  
  32.             android:gravity="center_horizontal" />  
  33.           
  34.     </LinearLayout>  
  35.   
  36.     <LinearLayout  
  37.         android:layout_width="0dp"  
  38.         android:layout_height="match_parent"  
  39.         android:gravity="center_vertical"  
  40.         android:layout_weight="1" >  
  41.   
  42.         <ImageView  
  43.             android:id="@+id/last_music"  
  44.             android:layout_width="0dp"  
  45.             android:layout_height="48dp"  
  46.             android:layout_weight="1"  
  47.             android:src="@drawable/music_previous" />  
  48.   
  49.         <ImageView  
  50.             android:id="@+id/play_pause_music"  
  51.             android:layout_width="0dp"  
  52.             android:layout_height="48dp"  
  53.             android:layout_weight="1"  
  54.             android:src="@drawable/music_play" />  
  55.   
  56.         <ImageView  
  57.             android:id="@+id/next_music"  
  58.             android:layout_width="0dp"  
  59.             android:layout_height="48dp"  
  60.             android:layout_weight="1"  
  61.             android:src="@drawable/music_next" />  
  62.     </LinearLayout>  
  63.   
  64. </LinearLayout>  

3.效果展示

  

4.源码下载

点击下载源码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值