Notification中显示ProgressBar组件

本文介绍如何在Android的通知栏中显示带有进度条的自定义通知。通过定义XML布局文件来设计通知样式,并使用RemoteViews结合多线程更新进度条状态,最终实现在通知栏显示动态变化的进度提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在Notification中显示ProgressBar,效果图:


 

2. 实现步骤:
    (1)在res/layout/目录中定义notification.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
    xmlns:android = "http://schemas.android.com/apk/res/android"  
    android:layout_width = "fill_parent"  
    android:layout_height = "fill_parent"  
    android:gravity = "center_vertical"  
    >        
    <ImageView   
        android:id = "@+id/image"     
        android:layout_width = "wrap_content"     
        android:layout_height = "fill_parent"     
        />  
    <ProgressBar  
        android:id = "@+id/progressBar"  
        android:layout_width = "180dip"  
        android:layout_height = "wrap_content"  
        style = "?android:attr/progressBarStyleHorizontal"  
        android:layout_gravity = "center_vertical"  
        />  
    <TextView  
        android:id = "@+id/tip"  
        android:layout_width = "wrap_content"     
        android:layout_height = "wrap_content"  
        android:textSize = "16px"  
        android:textColor = "#FF0000"  
        />  
</LinearLayout>  

    (2)主Activity实现:
public class NotificationActivity extends Activity {  
  
    private NotificationManager mNotificationManager;    
    private RemoteViews mRemoteViews;  
    private Intent mIntent;  
    private PendingIntent mPendingIntent;  
    private int mProgress = 0;  
    private Notification notification = new Notification();  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        /** 
         * Activity主布局,布局中存在一个Button,单击Button后启动多线程并向StatusBar发送Notification提醒。 
         */  
        setContentView(R.layout.main);  
        /** 
         * 获取Notification管理器。 
         */  
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
        /** 
         * 利用notification.xml文件创建RemoteView对象。 
         */  
        mRemoteViews = new RemoteViews(getPackageName(), R.layout.notification);  
        /** 
         * 单击Notification时发出的Intent消息。 
         */  
        mIntent = new Intent(NotificationActivity.this, 自定义Activity.class);  
        mPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, mIntent, 0);  
        /** 
         * 获取Button对象,设置单击事件,单击后启动多线程向StatusBar发送Notification提醒。 
         */  
        Button sendNotification = (Button) findViewById(R.id.sendNotification);  
        sendNotification.setOnClickListener(new OnClickListener() {  
            public void onClick(View view) {  
                /** 
                 * 设置Notification的Icon图标。 
                 */  
                notification.icon = R.drawable.icon;  
                mRemoteViews.setImageViewResource(R.id.image, R.drawable.icon);  
                /** 
                 * 启动多线程更新Notification提醒。 
                 */  
                new Thread(new Runnable() {  
                    public void run() {  
                        for (int i = 0; i < 20; i++) {  
                            mProgress = (i + 1) * 5;  
                            try {  
                                if (i < 19) {  
                                    Thread.sleep(1000);  
                                } else {  
                                    Thread.currentThread().interrupt();  
                                }  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                            Message message = new Message();  
                            mHandler.sendMessage(message);  
                        }  
                    }  
                }).start();  
            }  
        });  
    }  
  
    private Handler mHandler = new Handler() {  
        public void handleMessage(Message message) {  
            /** 
             * 设置RemoteView组件中进度条的进度值。 
             */  
            mRemoteViews.setProgressBar(R.id.progressBar, 100, mProgress, false);  
            mRemoteViews.setTextViewText(R.id.tip, "Download:" + mProgress + "%");  
            /** 
             * 给Notification设置布局。 
             */  
            notification.contentView = mRemoteViews;  
            /** 
             * 给Notification设置Intent,单击Notification会发出这个Intent。 
             */  
            notification.contentIntent = mPendingIntent;  
            /** 
             * 发送Notification提醒。 
             */  
            mNotificationManager.notify(0, notification);  
  
            super.handleMessage(message);  
        }  
    };  
}  转自:http://blog.youkuaiyun.com/mayingcai1987/archive/2011/03/06/6226685.aspx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值