如何收到仅由我的应用程序发布的下载完整广播?

时间:2013-02-18 18:56:36

标签: android broadcastreceiver

我使用了以下方法,但是如何将broadcastPermission设置为仅从应用程序本身接收广播?

context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), null, compressHandler);

2 个答案:

答案 0 :(得分:2)

您可以使用LocalBroadcastManager,例如

LocalBroadcastManager.getInstance(this).registerReceiver(onComplete,
  new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

在这种情况下,您只是使用DownloadManager.ACTION_DOWNLOAD_COMPLETE作为常量值,但不会从android DownloadManager类中接收任何内容

如果您想为广播see this question

设置权限

答案 1 :(得分:2)

您正试图解决两个问题:

  1. 您只想接收由DownloadManager广播的意图
  2. 您只想接收由您开始下载的结果
  3. 要解决第一个问题,您应该使用此字符串“android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS”作为此方法中的broadcastPermission:Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler)

    解决第二个问题的一种方法是存储方法DownloadManager.html#enqueue(android.app.DownloadManager.Request)返回的长ID。然后,当您收到意图时,请检查该ID是否与您请求的ID匹配。如果是,那么你知道这是你的要求。 Intent包含额外DownloadManager.html#EXTRA_DOWNLOAD_ID中的ID。