我有一个从 http 下载大文件的应用程序。为了处理这个问题,我使用了DownloadManager
.
要下载的文件太大,我希望用户能够将应用程序置于后台并在下载运行时自由使用他的手机。
在我编写的代码中,下载开始并且工作正常,但我需要每当用户在通知栏中单击下载文件的名称时,它必须向他显示下载列表(系统应用程序下载 - DownloadManager.ACTION_VIEW_DOWNLOADS
)。
我想要的是,当浏览器从 Web 下载文件时,我的应用程序将与默认浏览器的下载管理器完全相同。
你能帮我声明和编码BroadcastReceiver
吗?正如我所说,我需要一个BroadcastReceiver
始终运行的。
我试图这样做,但它不起作用:
在我的清单中有:
<manifest>
<application>
<receiver android:name="com.test.BroadcastReceiverDownloadManager">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
而我的 BroadcastReceiverDownloadManager.java 就是这个:
public class BroadcastReceiverDownloadManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.v("MY_APP","!!! onReceive !!!");
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action))
{
Intent i = new Intent(context,DownloadManager.class);
i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
context.startActivity(i);
}
}
}
它有时不处理点击事件,并且只要它正确处理点击,应用程序就会以这种方式崩溃:
02-27 19:13:04.098: E/AndroidRuntime(13274): FATAL EXCEPTION: main
02-27 19:13:04.098: E/AndroidRuntime(13274): java.lang.RuntimeException: Unable to start receiver com.my.app.name.services.BroadcastReceiverDownloadManager: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.my.app.name/android.app.DownloadManager}; have you declared this activity in your AndroidManifest.xml?
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2408)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.ActivityThread.access$1500(ActivityThread.java:139)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.os.Looper.loop(Looper.java:154)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.ActivityThread.main(ActivityThread.java:4944)
02-27 19:13:04.098: E/AndroidRuntime(13274): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 19:13:04.098: E/AndroidRuntime(13274): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 19:13:04.098: E/AndroidRuntime(13274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-27 19:13:04.098: E/AndroidRuntime(13274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-27 19:13:04.098: E/AndroidRuntime(13274): at dalvik.system.NativeStart.main(Native Method)
02-27 19:13:04.098: E/AndroidRuntime(13274): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.my.app.name/android.app.DownloadManager}; have you declared this activity in your AndroidManifest.xml?
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1634)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1510)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.ContextImpl.startActivity(ContextImpl.java:1100)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
02-27 19:13:04.098: E/AndroidRuntime(13274): at com.my.app.name.services.BroadcastReceiverDownloadManager.onReceive(BroadcastReceiverDownloadManager.java:29)
02-27 19:13:04.098: E/AndroidRuntime(13274): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2397)
02-27 19:13:04.098: E/AndroidRuntime(13274): ... 10 more