通知栏Notification自定义视图方法(显示进度条)

转自:http://www.devdiv.com/Android-%E9%80%9A%E7%9F%A5%E6%A0%8FNotification%E4%BD%BF%E7%94%A8%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%86%E5%9B%BE%E6%96%B9%E6%B3%95%EF%BC%88%E6%98%BE%E7%A4%BA%E4%B8%80%E4%B8%AA%E8%BF%9B%E5%BA%A6%E6%9D%A1ProgressBar%EF%BC%89-thread-46580-1-1.html


通知栏Notification使用自定义视图方法,这里以显示进度条ProgressBar为例,具体效果不上图了,请参考在Android Market下载软件时通知栏的效果。
布局main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Notification测试"
/>
<Button android:id="@+id/bt2"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="清除Notification"
/>
</LinearLayout>

自定义通知栏的布局:notification.xml文件,一个TextView显示下载中,下面一根进度条显示当前进度

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  >
<TextView android:id="@+id/down_tv" 
               android:layout_width="wrap_content"    
               android:layout_height="fill_parent" 
               android:textSize="20sp" 
               android:textColor="#000000"
               android:text="下载中"
               />   
<ProgressBar android:id="@+id/pb" 
             android:layout_width="260dip" 
             android:layout_height="wrap_content" 
             style="?android:attr/progressBarStyleHorizontal"
             android:layout_gravity="center_vertical"/>          
</LinearLayout>
程序代码:
package com.pocketdigi.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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class main extends Activity {
    /** Called when the activity is first created. */
        int notification_id=19172439;
        NotificationManager nm;
        Handler handler=new Handler();
        Notification notification;
        int count=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bt1=(Button)findViewById(R.id.bt1);
        bt1.setOnClickListener(bt1lis);
        Button bt2=(Button)findViewById(R.id.bt2);
        bt2.setOnClickListener(bt2lis);
        //建立notification,前面有学习过,不解释了,不懂看搜索以前的文章
        nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notification=new Notification(R.drawable.home,"图标边的文字",System.currentTimeMillis());
            notification.contentView = new RemoteViews(getPackageName(),R.layout.notification); 
            //使用notification.xml文件作VIEW
            notification.contentView.setProgressBar(R.id.pb, 100,0, false);
            //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
            //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
            Intent notificationIntent = new Intent(this,main.class); 
            PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); 
            notification.contentIntent = contentIntent;        
    }
    OnClickListener bt1lis=new OnClickListener(){

                @Override
                public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showNotification();//显示notification
                        handler.post(run);
                }

    };
    Runnable run=new Runnable(){

                @Override
                public void run() {
                        // TODO Auto-generated method stub        
                        count++;
                        notification.contentView.setProgressBar(R.id.pb, 100,count, false);
                        //设置当前值为count
                        showNotification();//这里是更新notification,就是更新进度条
                        if(count<100) handler.postDelayed(run, 200);
                        //200毫秒count加1
                }

    };
    OnClickListener bt2lis=new OnClickListener(){

                @Override
                public void onClick(View v) {
                        nm.cancel(notification_id);
                        //清除notification
                }

    };
    public void showNotification(){
            nm.notify(notification_id, notification);           
    }
}

效果图:



  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值