Android: how to open an apk file after downloading for auto-update?

后端 未结 2 693
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 12:45

I have an application that I\'d like to add auto-update functionality (it\'s not in the marketplace). I have all the code in place that would check to see if there is an up

2条回答
  •  醉梦人生
    2020-12-15 13:01

    The answer above was for pre-API-24.

    If your app targets API 24 or more (and it should), you need to use something else (otherwise you get FileUriExposedException, as described here) :

        File apkFile = new File(...);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", apkFile);
        intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    

    provider_paths.xml:

    
    
        
        
        
    
    

    where YOUR_PACKAGE_NAME is your app's package name.

    manifest:

        
            
        
    

提交回复
热议问题