我使用了以下方法,但是如何将broadcastPermission设置为仅从应用程序本身接收广播?
context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), null, compressHandler);
答案 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)
您正试图解决两个问题:
要解决第一个问题,您应该使用此字符串“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。