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,如需转载请自行联系原作者



相关文章
|
Android开发
Android 中Notification进度条一直弹出提示及提示音
Android 中Notification进度条一直弹出提示及提示音
264 0
|
10天前
|
XML 存储 Java
探索安卓开发之旅:从基础到进阶
【9月更文挑战第37天】安卓开发,一个充满无限可能的领域。它不仅关乎技术的深度与广度,更关乎开发者的成长与突破。本文将带你走进安卓开发的世界,从基础知识的学习到进阶技巧的掌握,一起感受编程的魅力与乐趣。
|
Android开发
Android的Notification研究
最近在研究Android,遇到了一些Notification(通知)的问题: 1、Notification如何传递参数 2、Notification如何区别化 3、从Intent(意图)寻找Activity(活动)说起,Android的Activity栈。
891 0
|
3天前
|
缓存 搜索推荐 Android开发
安卓开发中的自定义控件实践
【10月更文挑战第4天】在安卓开发的海洋中,自定义控件是那片璀璨的星辰。它不仅让应用界面设计变得丰富多彩,还提升了用户体验。本文将带你探索自定义控件的核心概念、实现过程以及优化技巧,让你的应用在众多竞争者中脱颖而出。
|
4天前
|
Java Android开发 Swift
安卓与iOS开发对比:平台选择对项目成功的影响
【10月更文挑战第4天】在移动应用开发的世界中,选择合适的平台是至关重要的。本文将深入探讨安卓和iOS两大主流平台的开发环境、用户基础、市场份额和开发成本等方面的差异,并分析这些差异如何影响项目的最终成果。通过比较这两个平台的优势与挑战,开发者可以更好地决定哪个平台更适合他们的项目需求。
19 1
|
20天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
6天前
|
Android开发
Android开发表情emoji功能开发
本文介绍了一种在Android应用中实现emoji表情功能的方法,通过将图片与表情字符对应,实现在`TextView`中的正常显示。示例代码展示了如何使用自定义适配器加载emoji表情,并在编辑框中输入或删除表情。项目包含完整的源码结构,可作为开发参考。视频演示和源码详情见文章内链接。
26 4
Android开发表情emoji功能开发
|
5天前
|
Web App开发 安全 程序员
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
多年的互联网寒冬在今年尤为凛冽,坚守安卓开发愈发不易。面对是否转行或学习新技术的迷茫,安卓程序员可从三个方向进阶:1)钻研谷歌新技术,如Kotlin、Flutter、Jetpack等;2)拓展新功能应用,掌握Socket、OpenGL、WebRTC等专业领域技能;3)结合其他行业,如汽车、游戏、安全等,拓宽职业道路。这三个方向各有学习难度和保饭碗指数,助你在安卓开发领域持续成长。
20 1
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
|
2天前
|
缓存 搜索推荐 Android开发
安卓开发中的自定义控件基础与进阶
【10月更文挑战第5天】在Android应用开发中,自定义控件是提升用户体验和界面个性化的重要手段。本文将通过浅显易懂的语言和实例,引导你了解自定义控件的基本概念、创建流程以及高级应用技巧,帮助你在开发过程中更好地掌握自定义控件的使用和优化。
17 10