前言今天分享的面試題是: Android在版本迭代中,總會進行很多改動,那么你熟知各版本都改動了什么內(nèi)容?又要怎么適配呢? Android4.4
Android5.0
Android6.0
如果你的應(yīng)用使用到了危險權(quán)限,比如在運行時進行檢查和請求權(quán)限。
Android 6.0 版移除了對 android {useLibrary 'org.apache.http.legacy'}有的小伙伴可能不熟悉這是啥,簡單說下:
所以說白了,其實就是一個請求網(wǎng)絡(luò)的項目框架。 Android 7.0
這一點其實就是限制了在應(yīng)用間共享文件,如果需要在應(yīng)用間共享,需要授予要訪問的URI臨時訪問權(quán)限,我們要做的就是注冊 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">
//代表設(shè)備的根目錄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 對于通知修改了很多,比如通知渠道、通知標志、通知超時、背景顏色。其中比較重要的就是通知渠道,其允許您為要顯示的每種通知類型創(chuàng)建用戶可自定義的渠道。 這樣的好處就是對于某個應(yīng)用可以把權(quán)限分成很多類,用戶來控制是否顯示哪些類別的通知。而開發(fā)者要做的就是必須設(shè)置這個渠道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, "廣告");
//創(chuàng)建group
notificationManager.createNotificationChannelGroup(group);
//channelId要唯一
String channelId = "channel_001";
NotificationChannel adChannel = new NotificationChannel(channelId,
"推廣信息", NotificationManager.IMPORTANCE_DEFAULT);
//補充channel的含義(可選)
adChannel.setDescription("推廣信息");
//將渠道添加進組(先創(chuàng)建組才能添加)
adChannel.setGroup(groupId);
//創(chuàng)建channel
notificationManager.createNotificationChannel(adChannel);
//創(chuàng)建通知時,標記你的渠道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) {
//安裝應(yīng)用
} else {
//跳轉(zhuǎn)至“安裝未知應(yīng)用”權(quán)限界面,引導用戶開啟權(quán)限
Uri selfPackageUri = Uri.parse("package:" + this.getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, selfPackageUri);
startActivityForResult(intent, 100);
}
}else {
//安裝應(yīng)用
}
}
//接收“安裝未知應(yīng)用”權(quán)限的開啟結(jié)果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
installAPK();
}
}
只有全屏不透明的 我們的處理辦法就是要么去掉設(shè)置方向的代碼,要么舍棄透明效果。 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 要求創(chuàng)建一個前臺服務(wù)需要請求 FOREGROUND_SERVICE 權(quán)限,否則系統(tǒng)會引發(fā) 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 環(huán)境中(比如Service,Application)啟動 Activity,否則會崩潰報錯,解決辦法就是加上 Intent intent = new Intent(this, TestActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); Android10
Android10中默認開啟了分區(qū)存儲,也就是沙盒模式。應(yīng)用只能看到本應(yīng)用專有的目錄(通過 如果需要關(guān)閉這個功能可以配置: android:requestLegacyExternalStorage="true" 分區(qū)存儲下,訪問文件的方法: 1)應(yīng)用專屬目錄 //分區(qū)存儲空間 val file = File(context.filesDir, filename) //應(yīng)用專屬外部存儲空間 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開始普通應(yīng)用不再允許請求權(quán)限android.permission.READ_PHONE_STATE。而且,無論你的App是否適配過Android Q(既targetSdkVersion是否大于等于29),均無法再獲取到設(shè)備IMEI等設(shè)備信息。 如果Android10以下設(shè)備獲取設(shè)備IMEI等信息,可以配置最大sdk版本: <uses-permission android:name="android.permission.READ_PHONE_STATE" android:maxSdkVersion="28"/> Android 11
沒錯,Android11強制執(zhí)行分區(qū)存儲,也就是沙盒模式。這次真的沒有關(guān)閉功能了,離Android11出來也有一段時間了,還是抓緊適配把。
改動了兩個API:getLine1Number()和 getMsisdn() ,需要加上READ_PHONE_NUMBERS權(quán)限
你一定很奇怪,為什么 哈哈,因為改動還是挺多的,所以給你推薦文章— 參考https:///post/6898176468661059597 拜拜
|
|
|