Notification
Notification.Builder
NotificationCompat.Builder

一开始给这三个类给搞糊涂了。

官网是这样解释的:
Notification:
A class that represents how a persistent notification is to be presented to the user using the NotificationManager.
The Notification.Builder has been added to make it easier to construct Notifications.
*构建Notifications主要用的类,发现好多方法都给移除给NotificationCompat.Builder替代了

Notification.Builder:
Builder class for Notification objects. Provides a convenient way to set the various fields of a Notification and generate content views using the platform's notification layout template.If your app supports versions of Android as old as API level 4, you can instead use NotificationCompat.Builder, available in the Android Support library.
*Notification.Builder是为了让开发者更容易构建出Notifications而诞生的。

NotificationCompat.Builder
Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts.
*NotificationCompat.Builder,由上面的加粗可以看出,NotificationCompat.Builder是解决Notification.Builder的兼容问题而诞生的。compat:兼容性

把这三个搞清楚之后,我直接用NotificationCompat.Builder来构建Notifications。

NotificationCompat.Builder mBuilder =           //Notification 的兼容类                new NotificationCompat.Builder(this)                .setSmallIcon(R.drawable.ic_launcher)   //若没有设置largeicon,此为左边的大icon,设置了largeicon,则为右下角的小icon,无论怎样,都影响Notifications area显示的图标                .setContentTitle("My notification") //标题                .setContentText("Hello World!")         //正文                .setNumber(3)                       //设置信息条数// .setContentInfo("3") //作用同上,设置信息的条数                .setLargeIcon(smallicon)           //largeicon,                .setDefaults(Notification.DEFAULT_SOUND)//设置声音,此为默认声音                .setVibrate(vT) //设置震动,此震动数组为:long vT[]={300,100,300,100}; 还可以设置灯光.setLights(argb, onMs, offMs)                .setOngoing(true)      //true使notification变为ongoing,用户不能手动清除,类似QQ,false或者不设置则为普通的通知                .setAutoCancel(true);             //点击之后自动消失

一个Notifications完成,现在已经可以把它显示出来,但我们继续coding,添加点击通知后的跳转。

实例化一个intent

Intent resultIntent = new Intent(this, ResultActivity.class);

实例化一个TaskStackBuilder ,用于添加动作,就像一个stack一样,一个一个压进去

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

添加父stack,添加下一个intent

stackBuilder.addParentStack(this);stackBuilder.addNextIntent(resultIntent);PendingIntent resultPendingIntent =                stackBuilder.getPendingIntent(                    0,                    PendingIntent.FLAG_UPDATE_CURRENT                );

把刚才的pending添加进去

mBuilder.setContentIntent(resultPendingIntent);

获得NotificationManager

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

mBuilder.build()会返回一个Notifications对象,1000为Notifications的id,可变动。就可以notify出来了。

mNotificationManager.notify(1000, mBuilder.build());

效果:

接下来是构建一个进度条Notifications

        new Thread(            new Runnable() {                @Override                public void run() {                    for(int i=0;i<100;i++){                        mBuilder.setProgress(100, i, false); //最后一个参数设置 determinate 还是 indeterminate ,                        //false的进度条是可以看到增加的,true是无限循环的,但设置为true的时候,前面连个参数可以忽略设置为0,0或者任意                        mNotificationManager.notify(1000, mBuilder.build());                        try {                            Thread.sleep(50);                        } catch (InterruptedException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                        }                    }                    mBuilder.setContentText("OK")                    .setDefaults(Notification.DEFAULT_ALL)   //设置后不会出现当进度完成之后奔溃,不设置会奔溃,原因不明。求大神指点                    .setProgress(0, 0, false);                    mNotificationManager.notify(1000, mBuilder.build());                }            }).start();

效果:

若mBuilder.setProgress(0, 0, true);
则:

更多相关文章

  1. Android(安卓)-- EditText插入表情
  2. Android(安卓)Studio3.0 Error:Execution failed for task ':app
  3. Android实现图片缩放示例
  4. Android(安卓)设置桌面背景
  5. android如何禁止安装第三方应用
  6. Android(安卓)Service一前台服务(四)
  7. Android(安卓)- 自定义标题栏(TitleBar)
  8. android中opengl es基本方法使用说明
  9. Android(安卓)- ScrollView 使用小计 里面嵌套的View 如何设置全

随机推荐

  1. Android(安卓)上从外部应用注入按键事件
  2. ListView 0基础篇
  3. Android笔记之:App列表之下拉刷新的使用
  4. 浅谈App原生开发、混合开发及HTML5开发的
  5. Android的HttpClient和WebView session不
  6. Android学习之Handler使用小结
  7. Android无源码调试Native代码(使用GDB)
  8. MVP模式在Android开发中的最佳实践
  9. Canvas的渲染效率对比与优化
  10. Android(安卓)手机应用开发经验 之 通过S