android 8.0新特性

背景介绍

谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办。大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0。谷歌在今年3 月21日发布 Android 新系统开发者预览版时已给新系统取名为 Android O。自2008 年发布以来, Android 依靠 Google 的生态运作,全球市场份额在2016年底已超过85% 。而近几年依靠 Android 发展起来的智能手机厂商不断增加, Android 生态大家庭也正在不断壮大。

1. android8.0新特性1-后台服务限制

以startService方式开启一个后台service之后,按home键使app进入后台,两分钟之后,后台service的ondestory方法会被调用, 后台service会销毁(8.0之前如果内存足够,后台service不会被销毁,但在8.0,无论内存是否足够,只要app在后台,大约两分后后台service就会销毁)
在 Android O 之前,创建前台服务的方式通常是先创建一个后台服务,然后将该服务推到前台。
Android O 有一项复杂功能;系统不允许后台应用创建后台服务。 因此,Android O 引入了一种全新的方法,即 Context.startForegroundService(),以在前台启动新服务。
在系统创建服务后,应用有五秒的时间来调用该服务的 startForeground() 方法以显示新服务的用户可见通知。
如果应用在此时间限制内未调用 startForeground(),则系统将停止服务并声明此应用为 ANR。

2.android8.0新特性2-广播启动限制

如果应用注册为接收广播,则在每次发送广播时,应用的接收器都会消耗资源。 如果多个应用注册为接收基于系统事件的广播,这会引发问题;触发广播的系统事件会导致所有应用快速地连续消耗资源,从而降低用户体验。
所以android 8.0对广播接收进行了限制, 不能以隐式意图的方式调用清单文件里注册的广播接收者(如果尝试这么做,并不会报错,但会发现没有效果,广播接收者)

3.android 8.0新特性3-通知渠道

Android O 引入了通知渠道,以提供统一的系统来帮助用户管理通知。当您以 Android O 为目标平台时,必须实现一个或多个通知渠道,以便向用户显示通知。

创建通知渠道的步骤:

  1. 创建 NotificationChannel 对象,并设置应用内唯一的通知 ID。
  2. 配置通知渠道的属性,比如提示声音等。
  3. 在 NotificationManager 中注册通知渠道对象。
    /**
     * 创建渠道
     * @param channelId 渠道id
     * @param channleName 渠道名
     * @return
     */
    public static NotificationChannel createNotificationChannel(String channelId, String channleName)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // The id of the channel.
            String id = channelId;
            // The user-visible name of the channel.
            CharSequence name = channleName;
            // The user-visible description of the channel.
            String description = "channel_description";
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel channel = new NotificationChannel(id, name, importance);
            // Configure the notification channel.
            channel.setDescription(description);
            channel.enableLights(true);
            // Sets the notification light color for notifications posted to this
            // channel, if the device supports this feature.
            channel.setLightColor(Color.BLUE);
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            channel.setShowBadge(true);
            return channel;
        }
        return null;
    }

在android 8.0中,当你创建一个通知时,必须指定该通知所归属的渠道,否则通知无法显示

 /**
     * 创建通知
     * @param context
     * @param title 通知标题 
     * @param content 通知
     t);

            builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
            builder.setContentTitle(title);// 设置通知的标题
            builder.setContentText(content);// 设置通知的内容
            builder.setChannelId(channelId);//设置channelid

            return builder.build();
        }
        return null;

    }

4.android 8.0新特性4-画中画模式

Android O 现已支持 Activity 的画中画模式。PIP 是一种多窗口显示模式,多用于视频播放,即你可以一边发微信一边看视频。

这里写图片描述

通常情况下,Activity是一个铺满全屏的界面,但是处于pip模式的activity可以只占据屏幕一部分。
将一个activity设置为pip模式,只需要将清单文件里注册的activity加入如下属性
 <activity android:name=".PipModeAct"  android:resizeableActivity="true"
            android:supportsPictureInPicture="true"
            android:configChanges=
                "screenSize|smallestScreenSize|screenLayout|orientation">

        </activity>

当你需要使activity进入画中画模式时,只需要调用该activity的enterPictureInPictureMode()方法,此时activity会显示为一个比较小的窗口,并且该activity的onPause()方法会被调用, 处于pause状态.

参考链接

https://developer.android.google.cn/preview/api-overview.html?hl=zh-cn

http://www.cnblogs.com/qyun/p/6715195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值