学习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();
关于通知就先记这么多吧,以后用到查看也方便。
书里的代码是:
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();
关于通知就先记这么多吧,以后用到查看也方便。
相关文章推荐
- jni中出现使用LOGD,出现undefined reference to '__android_log_print错误
- android纠正知识点
- Android中软解码和硬解码的优先级
- 自定义Android标题栏修改TitleBar的布局
- Android Adapter深入理解与优化
- Android开发将流读取成String返回的工具类
- Android用Intent切换Activity无反应
- 记录自己安装Android Studio
- Android Studio学习之签名打包APK
- No resource found that matches the given name 'android:Widget....
- Android——Activity练习
- android动画详解一 概述
- Android 软键盘详解
- Android View绘图机制
- Android软键盘弹出引起的各种不适终极解决方案
- android项目bug及解决方法小记录
- Android专项-针对Xposed hook密码框的防护
- Android应用反破解的思路
- Android_Fragment和activity传值(项目体现)
- Android SVN服务器的搭建及在AndroidStudio中使用SVN