啟動安裝apk本來網上一大堆,但我這個問題有點郁悶。本來我已經有啟動安裝的代碼,并且在相同設備上已經跑起來了。然而,另一個app使用相同代碼時卻不行了,不多說,代碼如下:
原本已經跑起來的各部分代碼:
app檔案內
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation files('libs/waterlibrary-debug.aar')
}
AndroidManifest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
當然也添加了權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
xml/file_paths內的代碼
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="download"/>
</paths>
</PreferenceScreen>
啟動安裝代碼
File mfile = new File(path);
Intent install = new Intent();
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setAction(Intent.ACTION_VIEW);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
//是android 7.0及以上的版本
Uri apkUri= FileProvider.getUriForFile(context,pageNameString+".fileprovider",mfile);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//如果沒有這句,在安裝時會出現“決議包出現錯誤的提示,實際就是Intent沒有讀取檔案的權限,這時是臨時給一下權限(因為這不是在主執行緒執行)
install.setDataAndType(apkUri,"application/vnd.android.package-archive");
}else {
//是android 7.0以下的版本//
install.setDataAndType(Uri.fromFile(mfile), "application/vnd.android.package-archive");
}
context.startActivity(install);
以上就是已經跑起來的代碼
而在另一個app中,我對比后發現,可能是IDE升級了我沒有注意
其中
app中的代碼變成這樣了
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('libs/waterlibrary-debug.aar')
}
對應的AndroidManifest.xml中變成了這樣
<provider
android:name= "android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
主要是這里不同
android:name= "android.support.v4.content.FileProvider"
之前是 android:name="androidx.core.content.FileProvider"
原本我想把參考的包都改了,但結果是行不通的,無故的把app中的參考改了,在AndroidManifest的參考可以了,但app都無法啟動了。
所以我就郁悶了,在現在的參考下,要怎樣才能啟動apk安裝呀!
急等,跪求支招!
uj5u.com熱心網友回復:
有什么錯誤提示?uj5u.com熱心網友回復:
編譯安裝app都沒有提示錯誤,下載檔案也沒有問題,啟動安裝包時直接閃退了,由于我用的模擬器是7以下版本,沒辦法dubug,as的模擬器老是啟動不了。
關鍵是另一個APP是正常在使用的,除了我說的代碼不同以外。
uj5u.com熱心網友回復:
急于投入使用,我已經將所有的參考升級為支持 android:name="androidx.core.content.FileProvider"現在升級可以正常安裝了
然而,原理不清楚,還是需要各位指教,畢竟網路的例子都用的android:name= "android.support.v4.content.FileProvider"
多學一點吧,書到用時方恨少啊!
uj5u.com熱心網友回復:
樓主 你的APP自動更新在線下載的時候,Android 9.0 能正常寫入嘛? 我的9.0版本手機寫入不了,權限都動態申請過了uj5u.com熱心網友回復:
我的可以啊,我用自己的榮耀手機測驗通過了的。代碼就是我貼出來的。
uj5u.com熱心網友回復:
你檢查下客戶手機有沒有給權限,我就遇到過客戶沒有通過讀寫權限導致寫入失敗的情況,手動開了權限就正常了。
uj5u.com熱心網友回復:
有這個檢查啊 沒有授權就一直彈框讓用戶選擇授權的
uj5u.com熱心網友回復:
//com.xxx.xxx.fileProvider 你兩個APP的這個值不會也是一樣的吧
Uri apkUri = FileProvider.getUriForFile(getApplicationContext(),
"com.xxx.xxx.fileProvider", saveFile);//在AndroidManifest中的android:authorities值
uj5u.com熱心網友回復:
你可以看一下,你前后參考的包不是一種,上面參考的是Androidx的包,下方是之前的support包,Google在API 28的時候開始將support包進行整合升級成Androidx,因此上面是android:name="androidx.core.content.FileProvider"而下方是
android:name= "android.support.v4.content.FileProvider"
只能選擇其中一種包,不能倆種包都是用,
uj5u.com熱心網友回復:
看的好眼熟,感覺想做相機那時候用的,忘了如何處理的了
uj5u.com熱心網友回復:
樓主 你的APP自動更新在線下載的時候,Android 9.0 能正常寫入嘛? 我的9.0版本手機寫入不了,權限都動態申請過了
你檢查下客戶手機有沒有給權限,我就遇到過客戶沒有通過讀寫權限導致寫入失敗的情況,手動開了權限就正常了。
有這個檢查啊 沒有授權就一直彈框讓用戶選擇授權的
有三個權限的,如果都有了,應該要能執行。我粘的代碼是測驗通過了的,只是對包的理解不夠,參考換了才出問題的。你逐行查一下
uj5u.com熱心網友回復:
OK 解決了 是因為權限的細分有問題,Google搞出來的幺蛾子,把sdcard_rw權限獨立移除了。。轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/78546.html
標籤:Android
上一篇:Android studio打開新建專案報錯,怎么解決啊,大佬們
下一篇:Android開發
