android源码分析之notification
2016-02-01 23:34
190 查看
参考资料
(1) Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)http://blog.csdn.net/x_i_a_o_h_a_i/article/details/32966767
(2) Android NotificationManager 和Notification的使用总结
http://www.cnblogs.com/stay/articles/1898963.html
我们参考一引起网上的资料,就可以大概的知道notification的应用了。
关键代码
1.系统的notification使用样例:
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder notification = new Notification.Builder(this) .setContentTitle("Custom notification:"+i) .setContentText("This is a notification:"+i) .setSmallIcon(R.drawable.ic_launcher) .setOngoing(true) .setAutoCancel(false) .setSmallIcon(R.drawable.ic_launcher) .setWhen(0); //notification.setPriority(Notification.PRIORITY_MAX); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0); notification.setContentIntent(contentIntent); long[] vibrate = {0,100,200,300}; notification.setVibrate(vibrate); Notification n =notification.build(); n.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; nm.notify(NOTIFY_ID, n);
2.自定义的notification使用样例:
//Notification notification = new Notification(icon, tickerText, when); Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher; notification.tickerText = "Notification:"+i; notification.when = System.currentTimeMillis(); RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); contentView.setTextViewText(R.id.title, "Custom notification:"+i); contentView.setTextViewText(R.id.text, "This is a custom layout:"+i); notification.contentView = contentView; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0); notification.contentIntent = contentIntent; notification.defaults |= Notification.DEFAULT_SOUND; // notification.sound = Uri.parse("file:/ sdcard /notification/ringer.mp3"); //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); notification.defaults |= Notification.DEFAULT_VIBRATE; long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate ; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS; // notification.priority = Notification.PRIORITY_MAX; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(NOTIFY_ID, notification);
3 关键源码类:
Notification.java—notificatoin的描述和创建类(base\core\java\android\app)
NotificationManager.java
屏蔽所有的通知:
我们可以设置一个标志位,对于在游戏模式下,可以屏蔽所有的notification,这是一个可以屏蔽notification干扰的好方法。
./frameworks/base/core/java/android/app/NotificationManager.java
public void notify(int id, Notification notification) { String isShowNotification = SystemProperties.get("tinno.notification.enable", "true"); if(isShowNotification.equals("true")){ notify(null, id, notification); } }
3.NotificationManagerService.java
这是一个对notification管理的服务,里有对notification的进行管理的核心方法。
NotificationManagerService.java
(base\services\core\java\com\android\server\notification)
AudioManager mAudioManager;—– 声音处理
Vibrator mVibrator;—– 振动处理
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories