在Android的Notification中显示进度条

来源:互联网 发布:了不起的node 编辑:程序博客网 时间:2024/05/16 09:29

http://vyphn.javaeye.com/blog/799126

 

最近研究了Notification,参考了一些文档,写了一些心得。在官方文档中得知在Android的Notification中可以显示进度条 ,就想做个例子试一下。在网上查了下,没有找到 
。决定自己写下,费了九牛二虎之力搞定了,现在拿出与大家分享下,废话少说,上代码:先自定义一个view 

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout           
  3. xmlns:android="http://schemas.android.com/apk/res/android"      
  4.               android:orientation="horizontal"      
  5.               android:layout_width="fill_parent"      
  6.               android:layout_height="fill_parent"      
  7.               android:padding="10dp"  
  8.               android:background="#880490FF"  
  9.               >       
  10.     <ImageView android:id="@+id/image"      
  11.                android:layout_width="wrap_content"      
  12.                android:layout_height="fill_parent"      
  13.                />       
  14.          <ProgressBar   
  15.                                  android:id="@+id/pb"  
  16.                                  android:layout_width="180dip"  
  17.                 android:layout_height="wrap_content"   
  18.                 style="?android:attr/progressBarStyleHorizontal"  
  19.                 android:layout_gravity="center_vertical"/>  
  20.      <TextView  
  21.                              android:id="@+id/tv"  
  22.                              android:layout_width="wrap_content"      
  23.                 android:layout_height="fill_parent"   
  24.                 android:textSize="16px"  
  25.                 android:textColor="#FF0000"/>             
  26. </LinearLayout>  


接着在Activity中写了逻辑代码: 
Java代码 
  1. package com.jieer;  
  2. import android.app.Activity;  
  3. import android.app.Notification;  
  4. import android.app.NotificationManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.RemoteViews;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.         //当前进度条里的进度值  
  17.         private int progress=0;  
  18.         private RemoteViews view=null;  
  19.         private Notification notification=new Notification();  
  20.         private NotificationManager manager=null;  
  21.         private Intent intent=null;  
  22.         private PendingIntent pIntent=null;//更新显示  
  23.         private Handler handler=new Handler(){  
  24.                 @Override  
  25.                 public void handleMessage(Message msg) {  
  26.                         // TODO Auto-generated method stub  
  27.                     view.setProgressBar(R.id.pb, 100, progress, false);  
  28.                     view.setTextViewText(R.id.tv, "下载"+progress+"%");//关键部分,如果你不重新更新通知,进度条是不会更新的  
  29.                     notification.contentView=view;  
  30.                         notification.contentIntent=pIntent;  
  31.                         manager.notify(0, notification);  
  32.                         super.handleMessage(msg);  
  33.                 }  
  34.               
  35.     };  
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.         manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  41.         view=new RemoteViews(getPackageName(),R.layout.custom_dialog);  
  42.                 intent=new Intent(MainActivity.this,NotificationService.class);  
  43.                 pIntent=PendingIntent.getService(MainActivity.this0, intent, 0);  
  44.         Button button=(Button)findViewById(R.id.bt);  
  45.         button.setOnClickListener(new Button.OnClickListener(){  
  46.   
  47.                         @Override  
  48.                         public void onClick(View v) {  
  49.                                 //通知的图标必须设置(其他属性为可选设置),否则通知无法显示  
  50.                                 notification.icon=R.drawable.icon;  
  51.                                 view.setImageViewResource(R.id.image, R.drawable.icon);//起一个线程用来更新progress  
  52.                                 new Thread(new Runnable(){  
  53.                                         @Override  
  54.                                         public void run() {  
  55.                                          for(int i=0;i<20;i++){  
  56.                                                 progress=(i+1)*5;  
  57.                                                 try {  
  58.                                                         if(i<19){  
  59.                                                                 Thread.sleep(1000);  
  60.                                                         }else {  
  61.                                                                 Thread.currentThread().interrupt();  
  62.                                                         }  
  63.                                                 } catch (InterruptedException e) {  
  64.                                                         e.printStackTrace();  
  65.                                                 }  
  66.                                                 Message msg = new Message();  
  67.                                                 handler.sendMessage(msg);  
  68.                                          }  
  69.                                         }  
  70.                                 }).start();  
  71.                         }  
  72.                   
  73.         });  
  74.     }  
  75. }  

原创粉丝点击