3

我有一个从 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
4

1 回答 1

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”应该与应用程序具有相同的包名。如果您想使用与应用程序不同的包,请完全指定它,例如 android:name="com.my.app.name.DownloadManager"

在我看来,包名称有些混乱。在日志中,它列出了活动的 com.my.app.name。接收器是 com.my.app.name.services.BroadcastReceiverDownloadManager,但在清单中你有 com.test.BroadcastReceiverDownloadManager。

实际 Java 文件中的包名是什么?它们需要与清单一致。

于 2013-02-28T18:52:00.463 回答