我正在尝试检测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)
this@Splash.startActivity(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似乎可以正常工作。
我想念什么?
答案 0 :(得分: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);
}