How to open an APK file for all Android versions

前端 未结 2 1229
暗喜
暗喜 2021-01-19 03:10

Background

So far, there was an easy way to install an APK file, using this intent:

    final Intent intent=new Intent(Intent.ACTION_VIEW)
                 


        
2条回答
  •  长情又很酷
    2021-01-19 03:54

    just for those who wonder how to finally install an APK properly, here:

    @JvmStatic
    fun prepareAppInstallationIntent(context: Context, file: File, requestResult: Boolean): Intent? {
        var intent: Intent? = null
        try {
            intent = Intent(Intent.ACTION_INSTALL_PACKAGE)//
                    .setDataAndType(
                            if (VERSION.SDK_INT >= VERSION_CODES.N)
                                androidx.core.content.FileProvider.getUriForFile(context, context.packageName + ".provider", file)
                            else
                                Uri.fromFile(file),
                            "application/vnd.android.package-archive")
                    .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
                    .putExtra(Intent.EXTRA_RETURN_RESULT, requestResult)
                    .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN)
                intent!!.putExtra(Intent.EXTRA_ALLOW_REPLACE, true)
        } catch (e: Throwable) {
        }
        return intent
    }
    

    manifest

    
      
    
    

    /res/xml/provider_paths.xml

    
    
      
      
      
    
    

提交回复
热议问题