状态栏使用prograssBar

本文介绍了一个在Android应用中实现自定义通知栏进度条的例子。通过创建Notification并使用RemoteViews更新进度条的状态,展示了如何在后台线程中更新UI组件,并确保主线程不被阻塞。此外还介绍了如何设置点击通知后的跳转行为。

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

private static final int PROGRESS = 0x1;
    private static final int MAX_PROGRESS = 100;
 
    private int mProgressStatus = 10;
 
    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.twolayout);
       
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        int icon = R.drawable.icon; //通知图标

        CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示

        long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

        //用上面的属性初始化Nofification

        Notification notification = new Notification(icon,tickerText,when);
       
       
       
        final RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
        contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
      
        notification.contentView = contentView;
     
 
        // Start file upload in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < MAX_PROGRESS) {
                    mProgressStatus += mProgressStatus;
 
                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
                        }
                    });
                }
            }
        }).start();
       
       
        Intent notificationIntent = new Intent(this,TestTwoDScrollView.class);

        PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        notification.contentIntent = contentIntent;

        manager.notify(1, notification);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值