监听来自其他应用程序的下载
Listen for downloads from other apps
我的应用程序应该监听下载(来自任何应用程序,例如浏览器),然后根据某些文件属性,通过通知提供一些操作。当应用程序当前不在前台 运行 时,这也应该有效,因此我正在使用静态定义的接收器(在 AndroidManifest.xml 中)进行操作。
但是,该应用程序从未收到 "download finished" 意图。这是我的代码:
下载接收器:
public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This notification is just for testing purposes
// to see if the DownloadReceiver works
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_check)
.setContentTitle("Your download has finished.")
.setContentText("Download finished");
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
AndroidManifest.xml:
<receiver
android:name=".DownloadReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
根据互联网上的一些解决方案,我添加了 android:exported=true
和 android:enabled=true
但都没有帮助。我还尝试在意图过滤器中使用常量名称 DownloadManager.DOWNLOAD_COMPLETE
,但这对我也没有帮助。还有其他解决方案吗?
经过深入研究,我发现这是not possible for privacy reasons. There is one possibility,虽然实现起来不太容易(它涉及到使用文件观察器)。
我的应用程序应该监听下载(来自任何应用程序,例如浏览器),然后根据某些文件属性,通过通知提供一些操作。当应用程序当前不在前台 运行 时,这也应该有效,因此我正在使用静态定义的接收器(在 AndroidManifest.xml 中)进行操作。 但是,该应用程序从未收到 "download finished" 意图。这是我的代码:
下载接收器:
public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This notification is just for testing purposes
// to see if the DownloadReceiver works
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_check)
.setContentTitle("Your download has finished.")
.setContentText("Download finished");
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
AndroidManifest.xml:
<receiver
android:name=".DownloadReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
根据互联网上的一些解决方案,我添加了 android:exported=true
和 android:enabled=true
但都没有帮助。我还尝试在意图过滤器中使用常量名称 DownloadManager.DOWNLOAD_COMPLETE
,但这对我也没有帮助。还有其他解决方案吗?
经过深入研究,我发现这是not possible for privacy reasons. There is one possibility,虽然实现起来不太容易(它涉及到使用文件观察器)。