Android / Kotlin - 本地BroadcastReceiver永远不会被DownloadManager激活

问题描述 投票:0回答:1

我正在尝试检测DownloadManager下载何时完成。不幸的是,我试图使用的本地BroadcastReceiver永远不会被调用。我见过多个类似的问题,但都没有解决我的问题;另外,如果BroadcastReceiver不是本地但在清单中声明它会被调用,所以我认为这不是DownloadManager的问题。

我无法使用外部BroadcastReceiver,因为我需要在下载完成后更新UI(更具体地说,打开另一个活动),据我所知,无法通过外部接收器完成(请纠正我,如果我错了)。

DownloadManager调用:

private fun download() {
    val mobileNetSSDConfigRequest: DownloadManager.Request = DownloadManager.Request(
            Uri.parse("https://s3.eu-west-3.amazonaws.com/gotcha-weights/weights/MobileNetSSD/MobileNetSSD.prototxt")
    )
    mobileNetSSDConfigRequest.setDescription("Downloading MobileNetSSD configuration")
    mobileNetSSDConfigRequest.setTitle("MobileNetSSD configuration")

    mobileNetSSDConfigRequest.setDestinationInExternalPublicDir(
            "Android/data/giorgioghisotti.unipr.it.gotcha/files/weights/", "MobileNetSSD.prototxt")

    val manager: DownloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    manager.enqueue(mobileNetSSDConfigRequest)
}

被授予权限时被调用:

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    when (requestCode) {
        PERMISSION_REQUEST_CODE -> {
            if (grantResults.isEmpty()
                    || grantResults[0] != PackageManager.PERMISSION_GRANTED
                    || grantResults[1] != PackageManager.PERMISSION_GRANTED
                    || grantResults[2] != PackageManager.PERMISSION_GRANTED
                    || grantResults[3] != PackageManager.PERMISSION_GRANTED
                    || grantResults[4] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this,
                        "Sorry, this app requires camera and storage access to work!",
                        Toast.LENGTH_LONG).show()
                finish()
            } else {
                val mobileSSDConfig = File(sDir + mobileNetSSDConfigPath)
                if (!mobileSSDConfig.exists()) download()
                else {
                    val myIntent = Intent(this, MainMenu::class.java)
                    this.startActivity(myIntent)
                }
            }
        }
    }
}

BroadcastIntent声明如此(Splash是活动的名称):

private val broadcastReceiver = object: BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        when (intent?.action) {
            DownloadManager.ACTION_DOWNLOAD_COMPLETE -> {
                val myIntent = Intent(this@Splash, MainMenu::class.java)
                [email protected](myIntent)
            }
        }
    }
}

并在活动的onCreate()方法中注册:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    LocalBroadcastManager.getInstance(this).registerReceiver(
            broadcastReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
    )

    ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.INTERNET,
            android.Manifest.permission.ACCESS_NETWORK_STATE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE), PERMISSION_REQUEST_CODE)
}

我尝试在onResume方法中注册接收器并在onCreate方法中声明它,没有变化。据我所知,我正在这样做,正如我在一些已接受的答案中看到的那样,我看不出问题。我知道从不调用BroadcastReceiver的事实,我检查了调试和各种控制台日志。由于正确下载文件并正确调用外部服务,因此DownloadManager似乎按预期工作。

我错过了什么?

kotlin broadcastreceiver android-download-manager
1个回答
0
投票

我有一个类似的问题,结果只是调用registerReceiver(broadcastReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),没有获得LocalBroadcastManager的实例解决了这个问题。

所以可能的问题是接收器是在错误的上下文对象上注册的。 [记得还要注意接收方的注册]

我这样做了

public void onResume() 
{
    super.onResume();
    registerReceiver(broadcastReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}

public void onPause() 
{
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}
© www.soinside.com 2019 - 2024. All rights reserved.