前言今天分享的面试题是: Android在版本迭代中,总会进行很多改动,那么你熟知各版本都改动了什么内容?又要怎么适配呢? Android4.4
Android5.0
Android6.0
如果你的应用使用到了危险权限,比如在运行时进行检查和请求权限。
Android 6.0 版移除了对 android {useLibrary 'org.apache.http.legacy'} 有的小伙伴可能不熟悉这是啥,简单说下:
所以说白了,其实就是一个请求网络的项目框架。 Android 7.0
这一点其实就是限制了在应用间共享文件,如果需要在应用间共享,需要授予要访问的URI临时访问权限,我们要做的就是注册 1)声明FileProvider。 <provider android:name="android.support.v4.content.FileProvider" android:authorities="app的包名.fileProvider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> <!--androidx版本类路径为:androidx.core.content.FileProvider--> 2)编写xml文件,确定可访问的目录 <paths xmlns:android="http://schemas./apk/res/android"> //代表设备的根目录new File("/"); <root-path name="root" path="." /> //context.getFilesDir() <files-path name="files" path="." /> //context.getCacheDir() <cache-path name="cache" path="." /> //Environment.getExternalStorageDirectory() <external-path name="external" path="." /> //context.getExternalFilesDirs() <external-files-path name="name" path="path" /> //getExternalCacheDirs() <external-cache-path name="name" path="path" /> </paths> 3)使用FileProvider if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri uri = FileProvider.getUriForFile(CameraActivity.this, "app的包名.fileProvider", photoFile); } else { Uri uri = Uri.fromFile(photoFile); } Android8.0
在 也就是说,以前你申请了
Android 8.0 对于通知修改了很多,比如通知渠道、通知标志、通知超时、背景颜色。其中比较重要的就是通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。 这样的好处就是对于某个应用可以把权限分成很多类,用户来控制是否显示哪些类别的通知。而开发者要做的就是必须设置这个渠道id,否则通知可能会失效。 private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //分组(可选) //groupId要唯一 String groupId = "group_001"; NotificationChannelGroup group = new NotificationChannelGroup(groupId, "广告"); //创建group notificationManager.createNotificationChannelGroup(group); //channelId要唯一 String channelId = "channel_001"; NotificationChannel adChannel = new NotificationChannel(channelId, "推广信息", NotificationManager.IMPORTANCE_DEFAULT); //补充channel的含义(可选) adChannel.setDescription("推广信息"); //将渠道添加进组(先创建组才能添加) adChannel.setGroup(groupId); //创建channel notificationManager.createNotificationChannel(adChannel); //创建通知时,标记你的渠道id Notification notification = new Notification.Builder(MainActivity.this, channelId) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle("一条新通知") .setContentText("这是一条测试消息") .setAutoCancel(true) .build(); notificationManager.notify(1, notification); } }
Android8.0以上必须使用新的窗口类型( if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY }else { mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT }
Android 8.0去除了“允许未知来源”选项,所以如果我们的App有安装App的功能(检查更新之类的),那么会无法正常安装。 <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> private void installAPK(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { boolean hasInstallPermission = getPackageManager().canRequestPackageInstalls(); if (hasInstallPermission) { //安装应用 } else { //跳转至“安装未知应用”权限界面,引导用户开启权限 Uri selfPackageUri = Uri.parse("package:" + this.getPackageName()); Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, selfPackageUri); startActivityForResult(intent, 100); } }else { //安装应用 } } //接收“安装未知应用”权限的开启结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 100) { installAPK(); } }
只有全屏不透明的 我们的处理办法就是要么去掉设置方向的代码,要么舍弃透明效果。 Android9.0
<application android:networkSecurityConfig="@xml/network_security_config"> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config> <!--或者在AndroidManifest.xml中配置: android:usesCleartextTraffic="true" -->
在6.0中取消了对 <uses-library android:name="org.apache.http.legacy" android:required="false"/>
Android 9.0 要求创建一个前台服务需要请求 FOREGROUND_SERVICE 权限,否则系统会引发 SecurityException。 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { startForegroundService(intentService); } else { startService(intentService); }
在9.0 中,不能直接非 Activity 环境中(比如Service,Application)启动 Activity,否则会崩溃报错,解决办法就是加上 Intent intent = new Intent(this, TestActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); Android10
Android10中默认开启了分区存储,也就是沙盒模式。应用只能看到本应用专有的目录(通过 如果需要关闭这个功能可以配置: android:requestLegacyExternalStorage="true" 分区存储下,访问文件的方法: 1)应用专属目录 //分区存储空间 val file = File(context.filesDir, filename) //应用专属外部存储空间 val appSpecificExternalDir = File(context.getExternalFilesDir(), filename) 2)访问公共媒体目录文件 val cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, "${MediaStore.MediaColumns.DATE_ADDED} desc") if (cursor != null) { while (cursor.moveToNext()) { val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID)) val uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id) println("image uri is $uri") } cursor.close() } 3)SAF(存储访问框架--Storage Access Framework) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) intent.addCategory(Intent.CATEGORY_OPENABLE) intent.type = "image/*" startActivityForResult(intent, 100) @RequiresApi(Build.VERSION_CODES.KITKAT) override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (data == null || resultCode != Activity.RESULT_OK) return if (requestCode == 100) { val uri = data.data println("image uri is $uri") } }
从Android10开始普通应用不再允许请求权限android.permission.READ_PHONE_STATE。而且,无论你的App是否适配过Android Q(既targetSdkVersion是否大于等于29),均无法再获取到设备IMEI等设备信息。 如果Android10以下设备获取设备IMEI等信息,可以配置最大sdk版本: <uses-permission android:name="android.permission.READ_PHONE_STATE" android:maxSdkVersion="28"/> Android 11
没错,Android11强制执行分区存储,也就是沙盒模式。这次真的没有关闭功能了,离Android11出来也有一段时间了,还是抓紧适配把。
改动了两个API:getLine1Number()和 getMsisdn() ,需要加上READ_PHONE_NUMBERS权限
你一定很奇怪,为什么 哈哈,因为改动还是挺多的,所以给你推荐文章— 参考https:///post/6898176468661059597 拜拜
|
|