此demo实现了Notification通知,包括一个下载进度条示例。
demo:下载地址
源码:
package fk.androiddemo_020;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
Button but,but2;
int notificationID;
Toast myToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.button);
but.setOnClickListener(this);
but2 = (Button) findViewById(R.id.button2);
but2.setOnClickListener(this);
notificationID=100;
//提前定义好的Toast
myToast=Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_LONG);
}
@Override
public void onClick(View v) {
if(v==but){
newMessageNotification();
}else{
new downloadNotificationThread().start();
}
//通知的ID增加,防止新的通知覆盖老的通知
notificationID++;
}
//下面一句使builder.build()在当前版本api(15)不报错,这个函数本来是api16的
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void newMessageNotification(){
//创建通知管理器对象
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建Notification.Builder对象
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//setSmallIcon这条属性是builder唯一必须设置的
builder.setSmallIcon(R.mipmap.ic_launcher);//通知上的图标(未展开时)
builder.setWhen(System.currentTimeMillis());//显示在通知上的时间
builder.setTicker("新消息");//通知弹出时的提示内容(模拟器上看不到)
builder.setContentTitle("您有一条新消息!!");//通知的标题
builder.setContentText("您中了500万大奖!!");//通知的内容
builder.setAutoCancel(true);//设置自动管理清除,例如点击后删除通知
Resources res = MainActivity.this.getResources();
//通知上的图片(通知列表展开时,没有时用SmallIcon代替),因为这个图片一般较大,用BitmapFactory处理参数
builder.setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher));
builder.setDefaults(Notification.DEFAULT_SOUND);//通知提示音
Intent intent = new Intent(MainActivity.this, TestActivity.class);//Intent对象
//封装Intent对象
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);//当通知被点击时就会执行其中的Intent
Notification n=builder.build();//用builder生成通知Notification对象
nm.notify(notificationID, n);//显示通知
}
class downloadNotificationThread extends Thread{
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
int currentID=notificationID;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void run(){
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("开始下载");
builder.setContentTitle("BOBO正在下载");
for(int incr=0;incr<=100;incr+=5){//循环更新这条通知的内容
builder.setContentText("下载进度:"+incr+"%");
//为通知添加进度条(总进度,当前进度,进度条样式为true时是一个无限循环条)
builder.setProgress(100,incr,false);
Notification n=builder.build();
nm.notify(currentID, n);
try {//暂停一秒模拟下载速度
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//下载完成后删除通知
nm.cancel(currentID);
//显示Toast,因为这个类没有继承Activity,所以Toast在外面定义
myToast.show();
}
}
}
运行截图: