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

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

相关文件下载在Linux公社的1号FTP服务器里,下载地址:

FTP地址:ftp://www.linuxidc.com

用户名:

密码:

在 2011年LinuxIDC.com\10月\10月\Android带进度条的通知栏源码

下载方法见

示例代码:

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 ;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwywjy.html