码迷,mamicode.com
首页 > 移动开发 > 详细

android notification 理解

时间:2016-07-22 21:17:13      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

notification简单使用

1.不推荐 , 已不使用

 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis());
    Intent intent = new Intent(this, NotificationActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
    PendingIntent.FLAG_CANCEL_CURRENT);
    //被删除
    notification.setLatestEventInfo(this, "This is content title","This is content text", pi);
    manager.notify(1, notification);

 

   2.系统推荐
  

 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, DD.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.sym_def_app_icon)    //图标
                .setContentTitle("标题")
                .setContentInfo("右下角")
                .setContentText("内容")
                .setAutoCancel(true)    //点击一下就消失
                .setContentIntent(pendingIntent)   //延迟意图
                .setTicker("刚出来是在手机最上方显示,一会就消失")
                .setWhen(System.currentTimeMillis())    //消息的时间
                .build();   //创建notification
//        notification.flags = Notification.FLAG_AUTO_CANCEL;      跟setAutoCancel一样作用
        //显示notification  第一个参数不能重复,否则就只能显示重复的最后一个notification
        manager.notify(1,notification);

 

3. 自定义notification
  

 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_main);
    remoteViews.setImageViewResource(R.id.imageView,android.R.drawable.sym_def_app_icon);
    remoteViews.setTextViewText(R.id.textText,"textview 的内容");
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, DD.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new NotificationCompat.Builder(this)
            .setAutoCancel(true)    //点击一下就消失
            .setContentIntent(pendingIntent)   //延迟意图
            .setContent(remoteViews)     //设置自定义的view
            .build();
    manager.notify(1,notification);

 

android notification 理解

标签:

原文地址:http://www.cnblogs.com/IT-lss/p/5696938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!