Android不同版本兼容性適配方法教程
Android 6
運行時權(quán)限動態(tài)申請,這里推薦郭霖的開源庫:https://github.com/guolindev/PermissionX
Android 7
在Android 7.0系統(tǒng)上,禁止向你的應(yīng)用外公開 file:// URI,如果一項包含文件 file:// URI類型的Intent離開你的應(yīng)用,應(yīng)用失敗,并出現(xiàn) FileUriExposedException異常,如調(diào)用系統(tǒng)相機拍照。若要在應(yīng)用間共享文件,可以發(fā)送 content:// URI類型的Uri,并授予URI 臨時訪問權(quán)限,使用FileProvider類。
使用FileProvider的大致步驟如下:
1.在res下創(chuàng)建xml目錄,在此目錄下創(chuàng)建file_paths.xml文件,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- 內(nèi)部存儲,等同于Context.getFilesDir,路徑:/data/data/包名/files目錄-->
<files-path
name="DocDir"
path="/" />
<!-- 內(nèi)部存儲,等同于Context.getCacheDir,路徑:/data/data/包名/cache目錄-->
<cache-path
name="CacheDocDir"
path="/" />
<!--外部存儲,等同于Context.getExternalFilesDir,路徑:/storage/sdcard/Android/data/包名/files-->
<external-files-path
name="ExtDocDir"
path="/" />
<!--外部存儲,等同于Context.getExternalCacheDir 路徑:/storage/sdcard/Android/data/包名/cache-->
<external-cache-path
name="ExtCacheDir"
path="/" />
</paths>
2.在manifest中注冊provider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.unclexing.exploreapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<!--exported:要為false,為true則會報安全異常。grantUriPermissions為true,表示授予URI臨時訪問權(quán)限-->
<meta-data
android:name="androidx.core.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
3.使用FileProvider
val file = File(
getExternalFilesDir(null),
"/temp/" + System.currentTimeMillis() + ".jpg"
)
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
}
//通過FileProvider創(chuàng)建一個content類型的Uri
val imageUri = FileProvider.getUriForFile(
this,
"com.unclexing.exploreapp.fileprovider", file
)
val intent = Intent()
//表示對目標(biāo)應(yīng)用臨時授權(quán)該Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.action = MediaStore.ACTION_IMAGE_CAPTURE
//將拍攝的照片保存到特定的URI
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivity(intent)
Android 8
Android 8.0 引入了通知渠道,允許為要顯示的每種通知類型創(chuàng)建用戶可自定義的渠道,用戶界面將通知渠道稱之為通知類別。
private fun createNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//如果要分組,groupId要唯一
val groupId = "group1"
val group = NotificationChannelGroup(groupId, "advertisement")
notificationManager.createNotificationChannelGroup(group)
//channelId唯一
val channelId = "channel1"
val notificationChannel = NotificationChannel(
channelId,
"promote information",
NotificationManager.IMPORTANCE_DEFAULT
)
//將渠道添加進(jìn)組,必須先創(chuàng)建組才能添加
notificationChannel.group = groupId
notificationManager.createNotificationChannel(notificationChannel)
//創(chuàng)建通知
val notification = Notification.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
.setContentTitle("A new notice")
.setContentText("Likes and follows")
.setAutoCancel(true)
.build()
notificationManager.notify(1, notification)
}
}Android 8.0以后不允許后臺應(yīng)用啟動后臺服務(wù),需要通過startForegroundService()指定為前臺服務(wù),應(yīng)用有五秒的時間來調(diào)用該 Service 的 startForeground() 方法以顯示可見通知。 如果應(yīng)用在此時間限制內(nèi)未調(diào)用startForeground(),則系統(tǒng)將停止 Service 并聲明此應(yīng)用為 ANR。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intent = Intent(this, UncleService::class.java)
startForegroundService(intent)
}
class UncleService : Service() {
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channel =
NotificationChannel("channelId", "channelName", NotificationManager.IMPORTANCE_HIGH)
manager.createNotificationChannel(channel)
val notification = Notification.Builder(this, "channelId")
.build()
startForeground(1, notification)
}
}
override fun onDestroy() {
super.onDestroy()
stopForeground(true)
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
}別忘了在manifest添加權(quán)限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Android 9
在Android 9中的網(wǎng)絡(luò)請求中,不允許使用http請求,要求使用https。
解決方案:
在 res 下新建一個xml目錄,然后創(chuàng)建一個名為:network_config.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
然后在Mainfiests appliction標(biāo)簽下配置該屬性
android:networkSecurityConfig="@xml/network_config"
這是一種簡單粗暴的方法,為了安全靈活,我們可以指定http域名,部分域名時使用http
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">csdn.example.com</domain>
</domain-config>
</network-security-config>Android 10
定位權(quán)限
用戶可以更好地控制應(yīng)用何時可以訪問設(shè)備位置,當(dāng)在Android 10上運行的應(yīng)用程序請求位置訪問時,會通過對話框的形式給用戶進(jìn)行授權(quán)提示,此時有兩種位置訪問權(quán)限:在使用中(僅限前臺)或始終(前臺和后臺)
新增權(quán)限 ACCESS_BACKGROUND_LOCATION
如果你的應(yīng)用針對 Android 10并且需要在后臺運行時訪問用戶的位置,則必須在應(yīng)用的清單文件中聲明新權(quán)限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
分區(qū)存儲
在Android 10之前的版本上,我們在做文件的操作時都會申請存儲空間的讀寫權(quán)限。但是這些權(quán)限完全被濫用,造成的問題就是手機的存儲空間中充斥著大量不明作用的文件,并且應(yīng)用卸載后它也沒有刪除掉。為了解決這個問題,Android 10 中引入了分區(qū)存儲的概念,通過添加外部存儲訪問限制來實現(xiàn)更好的文件管理。但是應(yīng)用得不徹底,因為我們可以在AndroidManifest.xml中添加android:requestLegacyExternalStorage="true"來請求使用舊的存儲模式,以此來做簡單粗暴地適配。但是我不推薦這樣做,因為Android 11強制執(zhí)行分區(qū)存儲機制,此配置已經(jīng)將會失效,所以還得老老實實地做下適配,直接看下面Android 11的適配吧。
Android 11
無需存儲權(quán)限即可訪問的有兩種,一是App自身的內(nèi)部存儲,一是App自身的自帶外部存儲。
對于存儲作用域訪問的區(qū)別就體現(xiàn)在如何訪問除此之外的目錄內(nèi)的文件。
強制執(zhí)行分區(qū)存儲
共享存儲空間存放的是圖片、視頻、音頻等文件,這些資源是公用的,所有App都能夠訪問它們。共享存儲空間里存放著圖片、視頻、音頻、下載的文件,App獲取或者插入文件的時候怎么區(qū)分這些類型呢?這個時候就需要MediaStore。比如想要查詢共享存儲空間里的圖片文件:
val cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
null
)
MediaStore.Images.Media.EXTERNAL_CONTENT_URI 意思是指定查詢文件的類型是圖片,并構(gòu)造成Uri對象,Uri實現(xiàn)了Parcelable,能夠在進(jìn)程間傳遞。
既然不能通過路徑直接訪問文件,那么如何通過Uri訪問文件呢?Uri可以通過MediaStore或SAF獲取。但是,需要注意的是:雖然也可以通過文件路徑直接構(gòu)造Uri,但是此種方式構(gòu)造的Uri是沒有權(quán)限訪問文件的。
現(xiàn)在我們來讀取/sdcard/目錄下的一個文本文件NewTextFile.txt,由于它不屬于共享存儲空間的文件,是屬于其它目錄的,因此不能通過MediaStore獲取,只能通過SAF獲取。
private fun openSAF() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
//指定選擇文本類型的文件
intent.type = "text/plain"
startActivityForResult(intent, 1)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && data != null) {
val uri = data.data
startRead(uri!!)
}
}
private fun startRead(uri: Uri) {
try {
val inputStream = contentResolver.openInputStream(uri)
val readContent = ByteArray(1024)
var len: Int
do {
len = inputStream!!.read(readContent)
if (len != -1) {
Log.d(tag, "file content:${String(readContent).substring(0, len)}")
}
} while (len != -1)
} catch (e: Exception) {
Log.d(tag, "Exception:${e.message}")
}
}由此可以看出,屬于"其它目錄"下的文件,需要通過SAF訪問,SAF返回Uri,通過Uri構(gòu)造InputStream即可讀取文件。
下面,我們再來寫入內(nèi)容到該文件中,還是需要通過SAF拿到Uri,拿到Uri后構(gòu)造輸出流。
private fun writeForUri(uri: Uri) {
try {
val outputStream = contentResolver.openOutputStream(uri)
val content = "my name is Uncle Xing"
outputStream?.write(content.toByteArray())
outputStream?.flush()
outputStream?.close()
} catch (e: Exception) {
Log.d(tag, "Exception:${e.message}")
}
}
SAF好處是:系統(tǒng)提供了文件選擇器,調(diào)用者只需指定要讀寫的文件類型,比如文本類型、圖片類型、視頻類型等,選擇器就會過濾出相應(yīng)文件以供選擇,使用簡單。
位置權(quán)限
Android 10請求ACCESS_FINE_LOCATION或 ACCESS_COARSE_LOCATION表示在前臺時擁有訪問設(shè)備位置信息的權(quán)限。在請求彈框還能看到始終允許,Android 11中,取消了始終允許選項,默認(rèn)不會授予后臺訪問設(shè)備位置信息的權(quán)限。Android 11將后臺獲取設(shè)備位置信息抽離了出來,通過ACCESS_BACKGROUND_LOCATION權(quán)限后臺訪問設(shè)備位置信息的權(quán)限,需要注意的一點是,請求ACCESS_BACKGROUND_LOCATION的同時不能請求其它權(quán)限,否則系統(tǒng)會拋出異常。官方給出的建議是先請求前臺位置信息訪問權(quán)限,再請求后臺位置信息訪問權(quán)限。
到此這篇關(guān)于Android不同版本兼容性適配方法教程的文章就介紹到這了,更多相關(guān)Android兼容性適配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android h5頁面獲取不到定位數(shù)據(jù)的問題解決
我們經(jīng)常會遇到onGeolocationPermissionsShowPrompt 已經(jīng)執(zhí)行,但仍然沒有獲取到定位數(shù)據(jù)的問題,所以本文給大家介紹了android h5頁面獲取不到定位數(shù)據(jù)的問題解決,需要的朋友可以參考下2024-11-11
Android開發(fā)案例手冊Application跳出dialog
這篇文章主要為大家介紹了Android開發(fā)案例手冊Application跳出dialog,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android開發(fā)之TextView控件用法實例總結(jié)
這篇文章主要介紹了Android開發(fā)之TextView控件用法,結(jié)合實例形式總結(jié)分析了TextView控件常用的屬性設(shè)置及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-02-02
???????Android?H5通用容器架構(gòu)設(shè)計詳解
這篇文章主要介紹了???????Android?H5通用容器架構(gòu)設(shè)計詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
Flutter路由跳轉(zhuǎn)參數(shù)處理技巧詳解
這篇文章主要為大家介紹了Flutter路由跳轉(zhuǎn)參數(shù)處理技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android編程實現(xiàn)二級下拉菜單及快速搜索的方法
這篇文章主要介紹了Android編程實現(xiàn)二級下拉菜單及快速搜索的方法,以實例形式較為詳細(xì)的分析了Android實現(xiàn)二級下拉菜單及快速搜索的布局與功能實現(xiàn)技巧,需要的朋友可以參考下2015-11-11

