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

2014-11-24 12:42:38 · 作者: · 浏览: 4

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


密码: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 ;
}