android之————通知——notification

来源:互联网 发布:淘宝怎么运营推广 编辑:程序博客网 时间:2024/06/15 11:42

1、对于一个通知而言,它显示的消息是有限的,一般仅用于提示一些概要信息。

2、但是一般简短的消息,并不能表达需要告诉用户的全部内容,所以需要绑定一个意图,当用户点击通知的时候,调用一个意图展示出一个Activity用来显示详细的内容。

3、而Notification中,并不使用常规的Intent去传递一个意图,而是使用PendingIntent。

4、NotificationManager是所有Notification的大管家,它的主要职责是加入/移除Notification。

通过获取系统服务来获取该对象:

      NotificationManager mNotificationManager = (NotificationManager)getSystemServic(Context.NOTIFICATION_SERVICE) ;

5、通知的一些常用属性:

均需要通知管理员的notifi()方法去刷新通知。

NotificationManager manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

/** * 方法一:初次学习通知里面的属性,系统的界面通知>>模拟短信通知 */public void SystemNotificationTest() {// 构造通知,并设置通知里面在状态栏显示的提醒信息,显示给用户界面的图片、标题、内容、时间.点击就消失通知等....NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setSmallIcon(R.drawable.m3);builder.setContentTitle("短信通知");builder.setContentText("你有1条未读短信,请点击查看");// builder.setAutoCancel(true);// 点击消失builder.setOngoing(true);// 设置为不可清除模式,流氓式通知builder.setTicker("你有新消息了!");// 任务栏里提示int kk = RENEW_ID++;builder.setNumber(kk);// 显示几条信息builder.setWhen(System.currentTimeMillis());// 显示当前时间Intent intentService = new Intent(this, MyTabHost.class);startService(intentService);
// 实例化PendingIntent的方法中的四个属性(上下文,不知道直接放0就行,Intent,直接用PendingIntent点,里面有4个属性);PendingIntent pending = PendingIntent.getActivity(this, 0,intentService, PendingIntent.FLAG_UPDATE_CURRENT);
// 将此通知设置进去,PendingIntent和Intent的属性一样,但是不是立即跳转,未决的
builder.setContentIntent(pending);// 刷新通知,前面那个参数是int型的,版本更新用的manger.notify(RENEW_ID, builder.build());}

6、如果你想点击通知进去过后停留在APP内,就要用到下面几行代码:

// 停留在那个APP应用里TaskStackBuilder taskstack = TaskStackBuilder.create(this);taskstack.addParentStack(MyTabHost.class);taskstack.addNextIntent(nextIntent);
<activity android:name="com.example.testone012.TabActivity.MyTabHost" >    <!--XML 文件里面也要添加  -->            <meta-data                android:name="android.support.PARENT_ACTIVITY"                android:value="com.example.testone005.mybaseadapter.s001.MybaseActivity" /></activity>

 

7、自定义一个通知,用到此类RemoteViews:

// ------自定义notification界面RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.activity_sharedpreferences_layout);builder.setContent(remoteView);//自定义通知里面按钮的事件处理remoteView.setOnClickPendingIntent(viewId, pendingIntent);


8、自定义进度条模拟下载的通知:

private Handler mHandler = new Handler();
public void pregressBarNotification() {final NotificationCompat.Builder progressBarActionBuilder = new NotificationCompat.Builder(this);progressBarActionBuilder.setTicker("XX已加入下载!").setSmallIcon(R.drawable.m3).setContentTitle("XX正在下载...").setContentText("正在下载,请稍后....").setWhen(System.currentTimeMillis());progressBarActionBuilder.setAutoCancel(true);new Thread(new Runnable() {public void run() {for (int progress = 0; progress < 101; progress++) {try {Thread.sleep(100);progressBarActionBuilder.setProgress(100, progress,false);// 刷新消息最好放到主线程去执行mHandler.post(new Runnable() {public void run() {manger.notify(RENEW_ID_PROGRESSBAR,progressBarActionBuilder.build());}});} catch (InterruptedException e) {e.printStackTrace();}}// 刷新消息最好放到主线程去执行mHandler.post(new Runnable() {public void run() {progressBarActionBuilder.setProgress(0, 0, false);progressBarActionBuilder.setContentTitle("下载完成!");progressBarActionBuilder.setContentText("请点击安装");manger.notify(RENEW_ID_PROGRESSBAR,progressBarActionBuilder.build());}});}}).start();// //模拟下载完成后发布通知信息//// manger.notify(RENEW_ID_PROGRESSBAR,// progressBarActionBuilder.build());//}



 



 

0 0