安卓适配--文件、悬浮窗、前台服务

本文详细对比了安卓10与安卓8系统的重要特性变化,包括安卓10的分区存储机制,每个应用程序拥有独立的私有目录,以及安卓8对悬浮框权限的调整,和前台服务申请方式的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接:https://www.jianshu.com/p/271bbd13bfcf

安卓 10

开始分区,每一个程序有自己的私有目录,在这个目录里读取、写入文件不需要权限

api  
    File file=content.getExternalFileDir("文件夹目录")
    File file1=new File(file.getAbsolutePath,"文件名")
    在自己私有目录生成了文件,但是如果不写入流,文件不可见。且如果在该目录下有同名文件夹,文件会生成失败
     
​

安卓8

安卓8对悬浮框的修改做了一些修改

参考链接 https://blog.csdn.net/LoveDou0816/article/details/79172637

在使用SYSTEM_ALERT_WINDOW权限弹出悬浮窗的应用无法在使用以下窗口类型来弹出悬浮窗

WindowManager.LayoutParams.TYPE_PRIORITY_PHONE
WindowManager.LayoutParams.TYPE_PHONE
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
​

把悬浮窗的type设置成 TYPE_APPLICATION_OVERPLAY

服务

参考链接 https://blog.csdn.net/Haienzi/article/details/81268022

在安卓8中前台服务的申请发生了改变

8.0生成Notification需要为notification设置一个渠道,通过渠道id

 private Notification getNotification() {
        Notification.Builder builder = new Notification.Builder(this);
//                .setContentTitle("投屏服务")
//
//                .setContentText("投屏服务正在运行...");
​
        //设置Notification的ChannelID,否则不能正常显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        Notification notification = builder.build();
        return notification;
    }

通过,notificationManager来生成渠道

  private void startForegroundService() {
​
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
​
        //创建•NotificationChannel
​
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        startForeground(1, getNotification());
    }

开启前台服务

8.0之前 startService(new Intent(this,Service.class))

8.0之后 startForegroundService(new Intent(this,Service.class))

停止前台服务

调用 stopForeground(true)移除正在运行的前台服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值