`
砺雪凝霜
  • 浏览: 151918 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自定义通知栏

阅读更多

摘自:http://blog.csdn.net/cstarbl/article/details/7200757

通知栏Notification使用自定义视图方法,这里以显示进度条ProgressBar为例,具体效果不上图了,请参考在Android Market下载软件时通知栏的效果。
布局main.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button android:id="@+id/bt1"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent"  
  10.         android:text="Notification测试"  
  11. />  
  12. <Button android:id="@+id/bt2"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_width="fill_parent"  
  15.         android:text="清除Notification"  
  16. />  
  17. </LinearLayout>  


自定义通知栏的布局:notification.xml文件,一个TextView显示下载中,下面一根进度条显示当前进度

 

 

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:orientation="vertical"  
  7.   >  
  8. <TextView android:id="@+id/down_tv"   
  9.                android:layout_width="wrap_content"      
  10.                android:layout_height="fill_parent"   
  11.                android:textSize="20sp"   
  12.                android:textColor="#000000"  
  13.                android:text="下载中"  
  14.                />     
  15. <ProgressBar android:id="@+id/pb"   
  16.              android:layout_width="260dip"   
  17.              android:layout_height="wrap_content"   
  18.              style="?android:attr/progressBarStyleHorizontal"  
  19.              android:layout_gravity="center_vertical"/>            
  20. </LinearLayout>  

程序代码:

[java] view plaincopy
  1. package com.pocketdigi.Notification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.RemoteViews;  
  14.   
  15. public class main extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.         int notification_id=19172439;  
  18.         NotificationManager nm;  
  19.         Handler handler=new Handler();  
  20.         Notification notification;  
  21.         int count=0;  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.   
  27.         Button bt1=(Button)findViewById(R.id.bt1);  
  28.         bt1.setOnClickListener(bt1lis);  
  29.         Button bt2=(Button)findViewById(R.id.bt2);  
  30.         bt2.setOnClickListener(bt2lis);  
  31.         //建立notification,前面有学习过,不解释了,不懂看搜索以前的文章  
  32.         nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  33.             notification=new Notification(R.drawable.home,"图标边的文字",System.currentTimeMillis());  
  34.             notification.contentView = new RemoteViews(getPackageName(),R.layout.notification);   
  35.             //使用notification.xml文件作VIEW  
  36.             notification.contentView.setProgressBar(R.id.pb, 100,0false);  
  37.             //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹  
  38.             //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)  
  39.             Intent notificationIntent = new Intent(this,main.class);   
  40.             PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);   
  41.             notification.contentIntent = contentIntent;          
  42.     }  
  43.     OnClickListener bt1lis=new OnClickListener(){  
  44.   
  45.                 @Override  
  46.                 public void onClick(View v) {  
  47.                         // TODO Auto-generated method stub  
  48.                         showNotification();//显示notification  
  49.                         handler.post(run);  
  50.                 }  
  51.   
  52.     };  
  53.     Runnable run=new Runnable(){  
  54.   
  55.                 @Override  
  56.                 public void run() {  
  57.                         // TODO Auto-generated method stub          
  58.                         count++;  
  59.                         notification.contentView.setProgressBar(R.id.pb, 100,count, false);  
  60.                         //设置当前值为count  
  61.                         showNotification();//这里是更新notification,就是更新进度条  
  62.                         if(count<100) handler.postDelayed(run, 200);  
  63.                         //200毫秒count加1  
  64.                 }  
  65.   
  66.     };  
  67.     OnClickListener bt2lis=new OnClickListener(){  
  68.   
  69.                 @Override  
  70.                 public void onClick(View v) {  
  71.                         nm.cancel(notification_id);  
  72.                         //清除notification  
  73.                 }  
  74.   
  75.     };  
  76.     public void showNotification(){  
  77.             nm.notify(notification_id, notification);             
  78.     }  
  79. }  


效果图:

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics