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

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;—– 振动处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android notificati