Android中通知的使用

2017-12-16  本文已影响0人  笔墨Android

不怕跌倒,所以飞翔

参考文献:
ASleepyCoder的Android Notification常见样式总结文章
vipra的Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

本文主要知识点概要:

1. 简单使用

1.1创建一个NotificationManager对象

NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

通过NotificationManager来管理通知内容;

1.2创建一个Notification对象

这个对象是通过构建者模式创建的,这里先贴代码,之后我在详细讲解常用的方法:

        NotificationCompat.Builder nb = new NotificationCompat.Builder(this, "1");
        nb.setContentTitle("通知的标题栏");
        nb.setContentText("通知栏内容");
        nb.setWhen(System.currentTimeMillis());
        nb.setSmallIcon(R.drawable.ic_stat_name);
        nb.setTicker("状态栏中提示的内容");
        nb.setDefaults(Notification.DEFAULT_VIBRATE);
        Notification notification = nb.build();

        mManager.notify(1,notification);

通过以上方法就可以成功的创建一个通知了,这里面具体的设置方法我在后面会详细讲解。

2.一些常用的设置方法

2.1当你创建通知的时候会使用到NotificationCompat来构建一个通知.常用到的方法如下:

2.2当你创建通知的时候会使用到Notification.常用到的方法如下:

当你使用builder构建出一个Notification的时候,也可以更改一些参数:
notification.flags = Notification.FLAG_AUTO_CANCEL; 设置提醒标识符,具体参数:

3.进阶使用

3.1 自定义的通知栏效果(RemoteViews)

Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件。否则会引起ClassNotFoundException异常

一般的步骤如下:

  1. 创建自定义视图
  2. 获取远程视图对象
  3. 设置PendingIntent来相应事件
  4. 发起Notification

3.1.1自定义带按钮的通知栏

其实别的方法都差不多,就是这里注意一下涉及资源的时候,其他的都是一样的.

3.1.2不带按钮的通知栏

其实和上面的都差不多,只是没有的相应的PendingIntent,多了一个设置文字的方法,这里就直接贴代码了!

        //先设定RemoteViews  
        RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.custom);  
        //设置对应IMAGEVIEW的ID的资源图片  
        view_custom.setImageViewResource(R.id.custom_icon, R.drawable.icon);  
        view_custom.setTextViewText(R.id.custom_title, "标题");  
        view_custom.setTextViewText(R.id.custom_content, "显示的内容"); 

3.2大图风格通知(4.1之前的版本不支持此功能)

只有在通知栏被展开时显示,何时展开呢?通知栏处在顶端,或者用户通过手势展开的时候显示.分为几种效果,但是写法其实都差不多,下面说说这几种风格,其实这些风格,比普通视图多出一个详情区域!

但是这个详情区域出现的方式各不相同,我的手机是按住下拉,有的手机是点击之后出现,有的是直接出现,这里自己多尝试一下就知道了!

3.2.1各个版本中的公共方法和各自的方法

先说一下公共方法!

其他都是单独存在的方法!

这个我觉得应该贴一下代码了!

    private void mediaStyle(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("MediaStyle");
        builder.setContentText("Song Title");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.notification));
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        Intent intent = new Intent(this,ImageActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this,1,intent,0);
        builder.setContentIntent(pIntent);
        //第一个参数是图标资源id 第二个是图标显示的名称,第三个图标点击要启动的PendingIntent
        builder.addAction(R.drawable.ic_previous_white,"",null);
        builder.addAction(R.drawable.ic_stop_white,"",null);
        builder.addAction(R.drawable.ic_play_arrow_white_18dp,"",pIntent);
        builder.addAction(R.drawable.ic_next_white,"",null);
        NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();
        style.setMediaSession(new MediaSessionCompat(this,"MediaSession",
                new ComponentName(MainActivity.this,Intent.ACTION_MEDIA_BUTTON),null).getSessionToken());
        //CancelButton在5.0以下的机器有效
        style.setCancelButtonIntent(pIntent);
        style.setShowCancelButton(true);
        //设置要现实在通知右方的图标 最多三个
        style.setShowActionsInCompactView(2,3);
        builder.setStyle(style);
        builder.setShowWhen(false);
        Notification notification = builder.build();
        manger.notify(TYPE_Media,notification);
    }

最后通过NotificationCompat.Builder.setStyle()设置进去就可以了

4.类似与QQ的在手机屏幕顶端出现的通知(只有在5.0以上系统中有效哦)

NotificationCompat.Builder.setFullScreenIntent(pIntent,true);

4.常见的问题

4.1如何取消掉通知栏上的通知

  1. 设置相应的flags = FLAG_AUTO_CANCEL
  2. 通过手动调用NotificationManager.cancle()方法关闭指定的通知或者通过NotificationManager.cancleAll()关闭所有通知.

关于常见的问题,等到以后用到的时候在去好好总结以下,现在总结的话或许会不那么完全,当你读到这篇文章后,要是有什么问题请和我联系,我会及时帮助你解决的,也能促进自己的提高...

上一篇 下一篇

猜你喜欢

热点阅读