展会信息港展会大全

自定义Notification并利用Handler更新Notification
来源:互联网   发布日期:2015-10-02 21:42:42   浏览:1878次  

导读:在消息通知的时候,我们经常用到两个控件Notification和Toast。特别是重要的和需要长时间显示的信息,用Notification最合适不过了。他可以在顶部显示一个图标以标示有了新的通知,当我们拉下通知栏的时候,可以看......

在消息通知的时候,我们经常用到两个控件Notification和Toast。特别是重要的和需要长时间显示的信息,用Notification最合适不过了。他可以在顶部显示一个图标以标示有了新的通知,当我们拉下通知栏的时候,可以看到详细的通知内容。

最典型的应用就是未看短信和未接来电的显示,还有QQ企鹅,我们一看就知道有一个未接来电或者未看短信,正在挂QQ。同样,我们也可以自定义一个Notification来定义我们自己的程序想要传达的信息。

Notification我把他分为两种,一种是默认的显示方式,另一种是自定义的。

在这里我主要介绍自定义Notification的用法,自定义Notification原理很简单,就是把通知区域自己写一个View,在代码里把Notification的contantview设置为我们自定义的view即可。

例如,这里有一个View

notification.xml

<?xml version= 1.0encoding= utf-8 ?>

<LinearLayout android:id= @+id/noti_lay

xmlns:android= http://schemas.android.com/apk/res/android

android:layout_width= wrap_content

android:layout_height= fill_parent

android:orientation= vertical

>

<LinearLayout android:id= @+id/top_lay

xmlns:android= http://schemas.android.com/apk/res/android

android:layout_width= wrap_content

android:layout_height= wrap_content

android:orientation= horizontal

>

<ImageView android:id= @+id/down_iv

android:layout_width= 30sp

android:layout_height= 30sp

android:src= @drawable/downnoti

/>

<TextView android:id= @+id/down_tv

android:layout_width= wrap_content

android:layout_height= fill_parent

android:textSize= 20sp

android:textColor= #000000

/>

</LinearLayout>

<LinearLayout android:id= @+id/progress_lay

xmlns:android= http://schemas.android.com/apk/res/android

android:layout_width= wrap_content

android:layout_height= fill_parent

android:orientation= horizontal

>

<TextView android:id= @+id/down_rate

android:layout_width= wrap_content

android:layout_height= fill_parent

android:textSize= 20sp

android:textColor= #000000

/>

<ProgressBar android:id= @+id/pb

android:layout_width= 260dip

android:layout_height= wrap_content

style= ?android:attr/progressBarStyleHorizontal

android:layout_gravity= center_vertical />

</LinearLayout>

</LinearLayout>

把他放在layout文件夹下。

这就是在通知区域要显示的东西,显示效果如下:

操作Notification的代码:

public Notification downloadNotification;

public NotificationManager downloadNM;

//..

/**

* 添加顶部下载通知

* liuzhao

*/

downloadNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

downloadNotification = new Notification(R.drawable.downnoti,apkname+ 下载,System.currentTimeMillis());

downloadNotification.contentView = newRemoteViews(getPackageName(),R.layout.notification);

//设置进度条的最大进度和初始进度

downloadNotification.contentView.setProgressBar(R.id.pb, 100,0, false);

//显示下载的包名

downloadNotification.contentView.setTextViewText(R.id.down_tv, apkname);

//显示下载的进度

downloadNotification.contentView.setTextViewText(R.id.down_rate,0% );

Intent notificationIntent = new Intent(tykmAndroid.this,tykmAndroid.class);

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

downloadNotification.contentIntent = contentIntent;

//显示通知

downloadNM.notify(downNotiID, downloadNotification);

//.

以上是让通知显示出来,下边要根据下载的进度,改变进度条的进度:

下载进程中的核心代码:

下载进程:

InputStream is = conn.getInputStream();

FileOutputStream fos = new FileOutputStream(file);

byte[] bt = new byte[1024];

int i = 0;

int progresCount = 0;

int times = 0;

while ((i = is.read(bt)) > 0) {

fos.write(bt, 0, i);

tykmAndroid.flagloading=1;

firstCat.flagloading=1;

long tempFileLength = file.length();

if((times == 512)||(tempFileLength == length)){

//更新通知中的进度条

int progress = (int)(tempFileLength*100/length);

if(progresCount<progress){

progresCount=progress;

System.out.println("progresCount="+progresCount);

//利用Handler在主线程中更新

Message msg = new Message();

msg.what = 6001;

Bundle b = new Bundle();

b.putInt("progressValue", progresCount);

b.putString("apkName", filename);

msg.setData(b);

tykmAndroid.tykmAndroid_Entity.handler.sendMessage(msg);

}

times=0;

continue;

}

times++;

}

fos.flush();

fos.close();

is.close();

conn.disconnect();

这里没有直接对通知进行更新,而是用了一个handler,让UI线程去更新。

UI线程中的Handler是这样写的:

case 6001://下载的时候更新Notification

Bundle b = msg.getData();

//得到进度条的当前值

int progressCount = b.getInt("progressValue");

//得到包名

String apkName = b.getString("apkName");

//下载未完成,显示进度条

if(progressCount !=100 ){

//设置进度条

downloadNotification.contentView.setProgressBar(R.id.pb, 100, progressCount, false);

//设置百分比

downloadNotification.contentView.setTextViewText(R.id.down_rate,""+progressCount+"%");

//更新进度条

}

//下载完成,显示下载完成

else{

downloadNotification.contentView.removeAllViews(R.id.progress_lay);

PendingIntent contentIntent =PendingIntent.getActivity(tykmAndroid.tykmAndroid_Entity, 0, getIntent(), 0);

downloadNotification.setLatestEventInfo(tykmAndroid.tykmAndroid_Entity,apkName,"下载完成",contentIntent);

}

downloadNM.notify(downNotiID, downloadNotification);

break;

这里有一段代码:

在Download线程里面:

if((times == 512)||(tempFileLength == length)){

这是防止频繁地更新通知,而导致系统变慢甚至崩溃。

非常重要。。。。。

赞助本站

人工智能实验室
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港