自定义Notification之进度条

实现一个带进度条的Notification

刚学android都要从头开始 慢慢研究 自己研究的东西记录一下

 

 

        private RemoteViews rViews;
 private Notification nNotification;
 private NotificationManager nManager;// 用来广播naNotification
/**
	 * 发送一个带进度条的notification
	 * @param filesize 记录文件的大小
	 * @param downloadsize 记录下载文件的大小, 时时获取
	 * @param result  获取当前下载的百分比 
	 */
	public void showNotification(int filesize, int downloadsize, int result) {

		
		nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nNotification = new Notification(R.drawable.icon, "您有新的消息",
				System.currentTimeMillis());
		// 实例化一个remoteviews对象给notification以达到自定义notification的效果
		rViews = new RemoteViews(getPackageName(),
				R.layout.noti_download_layout);
		rViews.setProgressBar(R.id.dl_progress, filesize, downloadsize, false);
		rViews.setTextViewText(R.id.dl_tv, "下载中:" + result + "%");
		nNotification.contentView = rViews;
		Log.i("contentview", ">>>>>>>>>>>>>>>>>>>>" + nNotification.contentView);
		// 设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
		Intent intent = new Intent(this, MainActivity.class);
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
		nNotification.contentIntent = pIntent;
		nManager.notify(R.layout.noti_download_layout, nNotification);
	}


布局界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/icon" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="match_parent" >

        <ProgressBar
            android:id="@+id/dl_progress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="10dp"
            
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp" />

        <TextView
            android:id="@+id/dl_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/dl_progress"
            android:text="进度"
            android:textSize="12sp" />
    </RelativeLayout>

</LinearLayout>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值