歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android帶進度條的通知欄(源碼)

Android一個帶有進度條的通知欄的DEMO,對初學Android的初學者是一個不錯的參考例子,代碼實現了點擊顯示按鈕就會在通知欄上出現一個有圖片和進度條的提示,點擊取消按鈕就會取消通知欄的提示通知。

相關文件下載在Linux公社的1號FTP服務器裡,下載地址:

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

用戶名:www.linuxidc.com

密碼:www.muu.cc

在 2011年LinuxIDC.com\10月\10月\Android帶進度條的通知欄源碼

下載方法見 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 ;
}

 

Copyright © Linux教程網 All Rights Reserved