Android带进度条的通知栏(源码)

Android一个带有进度条的通知栏的DEMO,对初学Android的初学者是一个不错的参考例子,代码实现了点击显示按钮就会在通知栏上出现一个有图片和进度条的提示,点击取消按钮就会取消通知栏的提示通知。

相关文件下载在安科网的1号FTP服务器里,下载地址:

密码:www.muu.cc

下载方法见 http://www.linuxidc.net/thread-1187-1-1.html

示例代码:

package com.hemowolf;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
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.RemoteViews;


public class main extends Activity implements OnClickListener{
    @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  _thread.interrupt();
  nm.cancel(NF_ID);
 }

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.btnShow).setOnClickListener(this);
        findViewById(R.id.btnCancel).setOnClickListener(this);

  nf =new Notification(R.drawable.icon,"带进度条的提醒",System.currentTimeMillis()) ;
  nf.icon = R.drawable.icon;
   
  nf.contentView= new RemoteViews(this.getPackageName(),R.layout.notification);
  nf.contentView.setProgressBar(R.id.ProgressBar01, 100, 0, false);
  nf.contentIntent=PendingIntent.getActivity( this, 0, new Intent(this,remoteview.class) ,0);
   
  nm = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    }
 
 public void onClick(View v) {
  switch(v.getId()){
  case R.id.btnShow:

   nm.notify(NF_ID, nf);
   
   _thread = new Thread ( new Runnable(){
    public void run() {
     while ( !Thread.currentThread().isInterrupted()){
      _handler.sendMessage(Message.obtain());
      try {
       Thread.currentThread().sleep(1000);
      } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       break ;
      }
     }
    }
   });
   _thread.start();
   break;
  case R.id.btnCancel:
   _thread.interrupt();
   nm.cancel(NF_ID);
   break;
  
  }
 }
 
 private Handler _handler =new Handler(){
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   _progress +=10;
   if (_progress>=100) _progress=0;
   nf.contentView.setProgressBar(R.id.ProgressBar01, 100, _progress, false);
   nm.notify(NF_ID, nf);
  }
 };

 private final int NF_ID=1111;
 private Notification nf ;
 private NotificationManager nm ;
 private int _progress=0;
 private Thread _thread ;
}

 

相关推荐