There is a very practical small software on the Android Market: Download crutch.
It solves the basic problem as follows: the default Android browser verifies mimetype when downloading files. If this mimetype is not "recognized ", the download manager rejects the download. Download crutch enables Download Manager to download both mimetype and mimetype that are identifiable.
All applications mounted to the system are amazing. Download crutch, a small software, is very clever. Theoretically, it can implement this function without writing a line of code.
To explore the working principles of download crutch, we should start with the working principles of browser and Download Manager.
The core of browser is the webview component, which processes all file types that webview can accept (or display. Such as HTML pages or various images. When webview encounters a mimetype he does not know, such as application/zip, it submits it to downloadmanager and requests downloadmanager for download.
When downloadmanager receives a download request, it immediately starts the HTTP thread for download. It constructs an intent first, ask if an application in the system is interested in the type of the file to be downloaded (or the file type can be post-downloaded ). If no application processes the mimetype, downloadmanager rejects the download. The key code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* * File: downloadservice. Java * Package: COM. Android. providers. downloads */ Intent mimetypeintent = new intent (intent. action_view ); Mimetypeintent. setdataandtype (URI. fromparts ("file", "", null), info. mimetype ); Resolveinfo rI = getpackagemanager (). resolveactivity (mimetypeintent, Packagemanager. match_default_only ); // If resolveinfo is not found, no application can process the mimetype. // Download will be terminated If (Ri = NULL ){ ...... Return; } |
Therefore, to solve the downloadmanager rejection problem, you only need to write an application and tell the system that all mimetype can be processed. So downloadcrutch was born.
To implement the downloadcrutch function, you only need to create an android project, write an empty activity, name it downloadcrutch, and register it in androidmanifest. xml. The key code is as follows:
1 2 3 4 5 6 7 8 |
<Activity Android: Name = ". downloadcrutch"> <Intent-filter> <Action Android: Name = "android. Intent. Action. View"/> <Category Android: Name = "android. Intent. Category. Default"/> <Data Android: Scheme = "file"/> <Data Android: mimetype = "*/*"/> </Intent-filter> </Activity> |
The Android: Scheme tag processes the URI structure of file: // XXXXX. The Android: mimetype tag can process any type of files.
How can this function be implemented without writing a line of Java code. However, downloadcrutch also writes some java code, which is also a very helpless choice, because after downloadmanager completes, it will pull an application that can process the mimetype to process the file, if you do not write a line of code, a black window activity will be pulled up, and the user experience is poor. Therefore, downloadcrutch adds a small toast in the code, prompting the user: "You just need to download it. How can you solve it by yourself ".