android源码分析之notification

参考资料

(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 关键源码类:

  1. Notification.java—notificatoin的描述和创建类
 (base\core\java\android\app)   
  1. 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;—– 振动处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值