Unity遇坑記 之 安卓包內打開apk覆蓋更新

最近各大市場開始要求將安卓的api等級設置成28,即需要我們開發者完成適配。

涉及到的升級改動可以說非常的少,一般只需要改改一些配置即可~

好吧,升級~,把AndroidManifest.xml中的targetSdkVersion改爲28~完成,再把打包的java sdk改成sdk9,jdk改成1.8,完成~~

重新打包,把一些報錯解決,完美出包。

試了一下,裏面的功能測試通過,重新提審,ok,過審上架。

但是,很容易忽略了一些東西,就是Android的權限問題,因爲unity接入的sdk權限都再接入文檔中有說明,但是有些權限是本身也需要的,例如想打開整包更新下載下來的apk包,在7.0以前,可以直接通過File接口讀取

public static void InstallApk(string path){

    File file = new File(path);
    if(!file.exists()) return;

    Intent inten = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    context.startActivity(intent);
}

在7.0以後需要通過FileProvider打開文件,使用FIleProvider還需要在AndroidManifest.xml配置路徑。

public static void InstallApk(string path){

    File file = new File(path);
    if(!file.exists()) return;
    
    Intent inten = new Intent(Intent.ACTION_VIEW);
    Uri uri = null;
    if(Build.VERSIONSDK_INT >= Build.VERSION_CODES.N){
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
    }else{
        uri = Uri.fromFile(file);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent);
}
<!-- 在AndroidManifest.xml 的 application 任意位置添加 定義FileProvider的配置 -->

<provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="填包名"+".provider"
  android:exported="false"
  android:grantUriPermission="true">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths" /> <!-- 這裏需要在這個路徑下放這個文件 -->
</provider>
<!-- 根據配置新建的 provider_path.xml -->

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-files-path>
    name="external_files_path"
    path="Download" />
  <external-path
    name="external_files"
    path="." />
</paths>

在8.0以上還有需要添加安裝外部位置來源的權限

在AndroidManifest.xml中添加

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

 

最後還要記得引入android-support-v4.jar這個包。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章