Home > Blockchain >  automatic update android app apk without playstore
automatic update android app apk without playstore

Time:10-14

I'm building an app for my several devices, the app won't be on app store.

I want to be able to update the app remotely automatically.

I figured out how to check the version against a server, however I still don't know how to automatically update the app whenever the is a new version.

Thanks a head!

CodePudding user response:

  • Firstly, as you said, you need to check the latest version, online and locally.

  • Then you need to put the apk newest apk online

  • Finally, if the online version is ahead the local, you need to download the file and update the app.

  • The easiest way to do it is to use the ota_update package to download and update the app

CodePudding user response:

I hope this solution is useful for you

  1. First you have to store the latest version on the server side using API.

  2. After that you have to compare the installed current app version to the server-side stored version if both are different and the current version lower than the server-side version at that time you have to perform the below code.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (!getPackageManager().canRequestPackageInstalls()) {
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1234);
         } else {
            downloadController = new DownloadController(DashboardNewActivity.this, url);
            downloadController.enqueueDownload();                       
         }
     }
    

Use this DownloadController.kt class

import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.core.content.FileProvider
import com.app.golden.BuildConfig
import com.app.golden.R
import java.io.File

class DownloadController(private val context: Context, private val url: String) {

   companion object {
      private const val FILE_NAME = "company.apk"
      private const val FILE_BASE_PATH = "file://"
      private const val MIME_TYPE = "application/vnd.android.package-archive"
      private const val PROVIDER_PATH = ".provider"
      private const val APP_INSTALL_PATH = "\"application/vnd.android.package-archive\""
   }

   fun enqueueDownload() {

     var destination = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()   "/"
     destination  = FILE_NAME

     val uri = Uri.parse("$FILE_BASE_PATH$destination")
     val file = File(destination)
     if (file.exists()) file.delete()

     val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
     val downloadUri = Uri.parse(url)
     val request = DownloadManager.Request(downloadUri)
     request.setMimeType(MIME_TYPE)
     request.setTitle(context.getString(R.string.app_name))
     request.setDescription(context.getString(R.string.downloading))

     // set destination
     request.setDestinationUri(uri)

     showInstallOption(destination, URI)
     // Enqueue a new download and same the referenced
     downloadManager.enqueue(request)
     Toast.makeText(context, context.getString(R.string.downloading), Toast.LENGTH_LONG)
        .show()
  }

  private fun showInstallOption(
    destination: String,
    uri: Uri
  ) {
    // set BroadcastReceiver to install app when .apk is downloaded
  val onComplete = object : BroadcastReceiver() {
       override fun onReceive(
           context: Context,
           intent: Intent
       ) {
           try {
               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                   val contentUri = FileProvider.getUriForFile(
                            context,
                            BuildConfig.APPLICATION_ID   PROVIDER_PATH,
                            File(destination)
                    )
                   val install = Intent(Intent.ACTION_VIEW)
                   install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                 install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    install.data = contentUri
                    context.startActivity(install)
                    context.unregisterReceiver(this)
                } else {
                    val install = Intent(Intent.ACTION_VIEW)
                    install.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    install.setDataAndType(
                            uri,
                            APP_INSTALL_PATH
                    )
                    context.startActivity(install)
                    context.unregisterReceiver(this)
                }
            } catch (e: Exception) {
                val data = e.message.toString()
                Log.e("Exception: ", data)
            }
        }
    }
    context.registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
  }
}
  • Related