[英]Intent to install APK file - Android Nougat
我進行了廣泛搜索,但沒有發現問題。 當您嘗試在Android Nougat
使用Intent
安裝APK file
,它根本不會安裝,並顯示以下警告:“解析軟件包時出現問題”。
例如,使用打開這種類型的文件( PDF
)的設置,它可以完美地打開PDF
文件。 但是要安裝。 apk
文件不起作用。
LogCat
沒有顯示任何錯誤,我LogCat
任何問題。
有什么事嗎
如下代碼:
清單 :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="br.com.xxxxxx.xxxxxx.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
xml /文件路徑:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="storage/emulated/0" path="."/>
活動:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(getContext().getFilesDir(), "app-debug.apk");
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}else{
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory() + "/app-debug.apk");
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
finish();
} catch (Exception e) {
e.getMessage();
}
}
拜托,這段代碼可能出什么問題了? 有人能幫我嗎?
我必須更改N(或更高)的意圖,並刪除類型名稱。 一旦完成,安裝將按預期進行。
所以對於N:
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(getContext().getFilesDir(), "app-debug.apk");
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setData(uri)
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
finish();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.