gpt4 book ai didi

android - 动态功能 Activity 未加载,卡在安装中

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:04 24 4
gpt4 key购买 nike

我正在尝试为我的项目实现应用程序包。在启动动态模块 Activity 时,它正在下载模块并尝试安装模块,但无法完全安装。它卡住了,如下图所示。 enter image description here

以下链接供引用。 https://github.com/googlesamples/android-dynamic-features

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import com.google.android.play.core.splitinstall.*
import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus
import com.lenskart.app.R

private const val modulePackageName = "com.test.feature"

private const val htoClassname = "$modulePackageName.hto.HtoActivity"

private const val atHomeClassname = "$modulePackageName.athome.AtHomeActivity"

class BundleActivity : AppCompatActivity() {

private val listener = SplitInstallStateUpdatedListener { state ->
val multiInstall = state.moduleNames().size > 1
state.moduleNames().forEach { name ->
when (state.status()) {
SplitInstallSessionStatus.DOWNLOADING -> {
displayLoadingState(state, "Downloading $name")
}
SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION -> {
startIntentSender(state.resolutionIntent().intentSender, null, 0, 0, 0)
}
SplitInstallSessionStatus.INSTALLED -> {
displayLoadingState(state, "Installed $name")
onSuccessfulLoad(name, launch = !multiInstall)
}

SplitInstallSessionStatus.INSTALLING -> displayLoadingState(state, "Installing $name")
SplitInstallSessionStatus.FAILED -> {
Log.e(TAG, "Error: ${state.errorCode()} for module ${state.moduleNames()}")
}

SplitInstallSessionStatus.CANCELED -> displayLoadingState(state, "Cancelled $name")
SplitInstallSessionStatus.UNKNOWN -> displayLoadingState(state, "Unknown thingy $name")
SplitInstallSessionStatus.PENDING -> displayLoadingState(state, "Pending $name")

}
}
}

private val moduleHto by lazy { "hto" }
private val moduleAtHome by lazy { "athome" }

private lateinit var manager: SplitInstallManager

private lateinit var progressBar: ProgressBar
private lateinit var progressText: TextView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bundle)
manager = SplitInstallManagerFactory.create(this)
initializeViews()
loadAndLaunchModule(moduleAtHome)
}

override fun onResume() {
manager.registerListener(listener)
super.onResume()
}

override fun onPause() {
manager.unregisterListener(listener)
super.onPause()
}

private fun loadAndLaunchModule(name: String) {
updateProgressMessage("Loading module $name")
if (manager.installedModules.contains(name)) {
updateProgressMessage("Already installed")
onSuccessfulLoad(name, launch = true)
return
}
val request = SplitInstallRequest.newBuilder()
.addModule(name)
.build()

manager.startInstall(request)

updateProgressMessage("Starting install for $name")
}

private fun onSuccessfulLoad(moduleName: String, launch: Boolean) {
if (true) {
when (moduleName) {
moduleHto -> launchActivity(htoClassname)
moduleAtHome -> launchActivity(atHomeClassname)
}
}

displayButtons()
}

private fun launchActivity(className: String) {
Intent().setClassName(packageName, className)
.also {
startActivity(it)
}
}

private fun displayLoadingState(state: SplitInstallSessionState, message: String) {
displayProgress()

progressBar.max = state.totalBytesToDownload().toInt()
progressBar.progress = state.bytesDownloaded().toInt()

updateProgressMessage(message)
}

private fun initializeViews() {
progressBar = findViewById(R.id.progress_bar)
progressText = findViewById(R.id.progress_text)
}

private fun updateProgressMessage(message: String) {
if (progressText.visibility != View.VISIBLE &&
progressBar.visibility != View.VISIBLE) displayProgress()
progressText.text = message
}

private fun displayProgress() {
progressText.visibility = View.VISIBLE
progressBar.visibility = View.VISIBLE
}

private fun displayButtons() {
progressText.visibility = View.GONE
progressBar.visibility = View.GONE
}

}

private const val TAG = "DynamicFeatures"

最佳答案

根据文档,您还需要启用 SplitCompat。您有 3 个选择:

  • 选项 1:通过在 AndroidManifest.xml 中定义 SplitCompatApplication 作为默认应用程序

    <application
    ...
    android:name="com.google.android.play.core.splitcompat.SplitCompatApplication" >
    </application>
  • 选项 2:让您当前的应用程序类扩展 SplitCompatApplication。

    public class MyApplication extends SplitCompatApplication {
    ...
    }
  • 选项 3:从您的应用程序覆盖 attachBaseContext 方法。

    @Override
    protected void attachBaseContext(Context context) {
    super.attachBaseContext(context);
    // Emulates installation of future on demand modules using SplitCompat.
    SplitCompat.install(this);
    }

Source

关于android - 动态功能 Activity 未加载,卡在安装中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52480833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com