实现一个带进度条的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>