您的位置:首页 > 移动开发 > Android开发

学习android笔记(2):通知(Notification)的新用法

2016-03-17 20:41 543 查看
寒假看完毕老师的Java视频,回到学校一有空我就抱着这本书去图书馆,不知不觉看的已经过半了。总的来说,这本书确实不错,内容通俗易懂,很适合新手,不过当我看到通知那一章时,我被郁闷到了,Notification 这个类被废弃了。

书里的代码是:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis());

notification.setLatestEventInfo(this, "This is content title", "This is content text", null);

manager.notify(1, notification);

查了API后知道通知的最新用法:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification.Builder nb =  new Notification.Builder(this);//先建立一个Builder对象
nb.setSmallIcon(R.drawable.ic_launcher);//设置图片
nb.setTicker("Notification comes");//设置Ticker,即提示栏中内容
Intent notificationIntent = new Intent(this,MainActivity.class);//设置点击通知要打开的界面
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
nb.setContentTitle("This is contentTitle");//设置通知的标题
nb.setContentText("This is content");//设置通知内容
nb.setContentIntent(pendingIntent);
Notification notification= nb.build();//通过调用Builder的build()方法建立一个通知实例;                     

manager.notify(1, notification);

后来我发现,我没设置通知里的时间,但还是会出现,查了下Builder的源码:

 public Builder(Context context) {

       

            mContext = context;

            // Set defaults to match the defaults of a Notification

            mWhen = System.currentTimeMillis();

            mAudioStreamType = STREAM_DEFAULT;

            mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;

            mPriority = PRIORITY_DEFAULT;

            mPeople = new ArrayList<String>();

            mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?

                    NotificationColorUtil.getInstance(mContext) : null;

        }

里面就已经定义好了When:

  mWhen = System.currentTimeMillis();

关于通知就先记这么多吧,以后用到查看也方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: