我有一个从http下载大文件的应用程序。
为了解决这个问题,我使用了DownloadManager
。
要下载的文件太大,我希望用户能够将应用程序放在后台并在下载运行时自由使用他的手机。
在我编写的代码中,下载开始并且工作正常,但是每当用户点击通知栏中的下载文件名时,我都需要它,它必须向他显示下载列表(系统应用程序下载 - DownloadManager.ACTION_VIEW_DOWNLOADS
)。
我想要的是,当浏览器从网上下载文件时,我的应用程序将与默认浏览器的下载管理器完全相同。
您可以帮助我声明和编码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);
}
}
}
它有时无法处理click事件,只要它正确处理了点击,应用程序就会以这种方式崩溃:
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
答案 0 :(得分:0)
正如日志中所述:
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?
要发布活动,您必须声明它。打开文件AndroidManifest.xml,并确保在应用程序标记内声明了活动:
<activity
android:name=".DownloadManager"
android:label="@string/app_name"
>
</activity>
“.DownloadManager”应与应用具有相同的包名称。如果您想使用与应用不同的套餐,请完全指定,例如: 机器人:名称= “com.my.app.name.DownloadManager”
在我看来,对包名有一些困惑。在日志中,它列出了活动的com.my.app.name。接收器是com.my.app.name.services.BroadcastReceiverDownloadManager,但是在清单中你有com.test.BroadcastReceiverDownloadManager。
实际Java文件中的软件包名称是什么?他们需要与清单保持一致。