Android 为Notification加上一个进度条

简介:

代码都加了注释了,整个操作都比较简单,上代码:

 

package  com.notification;

import  android.app.Activity;
import  android.app.Notification;
import  android.app.NotificationManager;
import  android.app.PendingIntent;
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.Button;
import  android.widget.RemoteViews;
import  android.widget.Toast;

public   class  nofificationActivity  extends  Activity  implements  OnClickListener {

    
private   static   final   int  NOTIFICATION_ID  =   0x12 ;
    
private  Notification notification  =   null ;
    
private  NotificationManager manager  =   null ;

    
public  Handler handler;
    
private   int  _progress  =   0 ;
    
private  Thread thread  =   null ;
    
private   boolean  isStop  =   false ;

    
//  当界面处理停止的状态 时,设置让进度条取消
    @Override
    
protected   void  onPause() {
        
//  TODO Auto-generated method stub
        isStop  =   false ;
        manager.cancel(NOTIFICATION_ID);

        
super .onPause();
    }

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

        Button btn 
=  (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(
this );
        notification 
=   new  Notification(R.drawable.icon,  " 带进条的提醒 " , System
                .currentTimeMillis());
        notification.icon 
=  R.drawable.icon;

        
//  通过RemoteViews 设置notification中View 的属性
        notification.contentView  =   new  RemoteViews(getApplication()
                .getPackageName(), R.layout.custom_dialog);
        notification.contentView.setProgressBar(R.id.pb, 
100 0 false );
        notification.contentView.setTextViewText(R.id.tv, 
" 进度 "   +  _progress
                
+   " % " );
        
//  通过PendingIntetn
        
//  设置要跳往的Activity,这里也可以设置发送一个服务或者广播,
        
//  不过在这里的操作都必须是用户点击notification之后才触发的
        notification.contentIntent  =  PendingIntent.getActivity( this 0 ,
                
new  Intent( this , remoteView. class ),  0 );
        
//  获得一个NotificationManger 对象,此对象可以对notification做统一管理,只需要知道ID
        manager  =  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }

    @Override
    
public   void  onClick(View v) {
        
//  TODO Auto-generated method stub
        isStop  =   true ;
        manager.notify(NOTIFICATION_ID, notification);
        thread 
=   new  Thread( new  Runnable() {

            @Override
            
public   void  run() {
                Thread.currentThread();
                
//  TODO Auto-generated method stub
                 while  (isStop) {
                    _progress 
+=   10 ;
                    Message msg 
=  handler.obtainMessage();
                    msg.arg1 
=  _progress;
                    msg.sendToTarget();

                    
try  {
                        Thread.sleep(
500 );
                    } 
catch  (InterruptedException e) {
                        
//  TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();

        handler 
=   new  Handler() {
            @Override
            
public   void  handleMessage(Message msg) {
                
//  TODO Auto-generated method stub
                notification.contentView.setProgressBar(R.id.pb,  100 , msg.arg1,
                        
false );
                notification.contentView.setTextViewText(R.id.tv, 
" 进度 "
                        
+  msg.arg1  +   " % " );
                manager.notify(NOTIFICATION_ID, notification);

                
if  (msg.arg1  ==   100 ) {
                    _progress 
=   0 ;
                    manager.cancel(NOTIFICATION_ID);
                    isStop 
=   false ;
                    Toast.makeText(nofificationActivity.
this " 下载完毕 " 1000 )
                            .show();
                }
                
super .handleMessage(msg);
            }
        };
    }
}

 

 本文转自 terry_龙 51CTO博客,原文链接:http://blog.51cto.com/terryblog/386042,如需转载请自行联系原作者



相关文章
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
357 1
|
Android开发 UED
Android 实现通知栏和进度条效果(适用于Android8.0以上)
Android 实现通知栏和进度条效果(适用于Android8.0以上)
495 0
|
12月前
|
XML 搜索推荐 Android开发
Android改变进度条控件progressbar的样式(根据源码修改)
本文介绍了如何基于Android源码自定义ProgressBar样式。首先分析了系统源码中ProgressBar样式的定义,发现其依赖一张旋转图片实现动画效果。接着分两步指导开发者实现自定义:1) 模仿源码创建一个旋转动画XML文件(放置在drawable文件夹),修改图片为自定义样式;2) 在UI控件中通过`indeterminateDrawable`属性应用该动画。最终实现简单且个性化的ProgressBar效果,附带效果图展示。
678 2
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
432 0
|
XML Java Android开发
Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)
Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)
2089 0
|
Android开发
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
538 0
|
API Android开发
Android 监听Notification 被清除实例代码
Android 监听Notification 被清除实例代码
|
Java API Android开发
19. 【Android教程】进度条 ProgressBar
19. 【Android教程】进度条 ProgressBar
1173 4
|
API Android开发
31. 【Android教程】状态栏通知:Notification
31. 【Android教程】状态栏通知:Notification
1909 1
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
1399 1

热门文章

最新文章