First:
The android player supports "download and play". When you click "MP3 download link" in the system browser, it will automatically play the song and save it to the local device (I don't know if this is the case with a third-party browser, I think the system browser will automatically recognize the MP3 download link, and call the system player to play the video ).
Similar to this process, in the author's music player, when the user selects "Download song", it will be transferred to a webview, here, I direct the initial webview URL to "Custom". When I click the MP3 download link, the music will be downloaded to the root directory of sdcard. The code of the activity where webview is located is as follows:
Java code:
- Setcontentview (R. layout. Web );
- Web = (webview) findviewbyid (R. Id. Web );
- Web. setwebviewclient (New downloadwebviewclient (this ));
- Websettings S = web. getsettings ();
- S. setsaveformdata (false );
- S. setsavepassword (false );
- S. setusewideviewport (true );
- S. setjavascriptenabled (true );
- S. setlighttouchenabled (true );
- Web. setwebchromeclient (New webchromeclient (){
- Public void onprogresschanged (webview view, int progress ){
- // Activity and webview determine the progress of the progress bar based on the loading degree
- // The progress bar disappears automatically when it is loaded to 100%
- Context. setprogress (Progress * 100 );
- }
- });
- Web. loadurl ("http://www.top100.cn /");
Copy code
Web. setwebviewclient (New downloadwebviewclient (this); among them, downloadwebviewclient enables us to download the key MP3 file. It inherits from webviewclient. Here we override its public Boolean shouldoverrideurlloading (webview, string URL) method: In the method, we determine whether the clicked link is "Download MP3 files". If it is true, start a service to download MP3 files. The Code is as follows:
Java code:
- Public class downloadwebviewclient extends webviewclient {
- Private context;
- Public downloadwebviewclient (context ){
- This. Context = context;
- }
- @ Override
- Public Boolean shouldoverrideurlloading (webview view, string URL ){
- Log. I ("info", "open an URL ");
- String urlstr = "";
- // Store the decoded URL
- // For UTF-8 encoding
- If (isutf8url (URL )){
- Urlstr = utf8urldecode (URL );
- // If it is not UTF-8 encoded
- } Else {
- Urlstr = urldecoder. Decode (URL );
- }
- // If the link is to download the MP3 file in top100.cn
- If (URL. substring (URL. Length ()-4). Equals (". MP3") & URL. substring (221). Equals ")){
- Log. I ("info", "MP3 file ");
- String ss [] = urlstr. Split ("/");
- String musicname = ss [ss. Length-1]; // obtain the full name (including the suffix) of the music file)
- Log. I ("info", "musicfile:" + musicname );
- // Pass the download link and file name to the download Module
- Intent intent = new intent (context, downloadservice. Class );
- Intent. putextra ("url", URL );
- Intent. putextra ("musicname", musicname );
- Context. startservice (intent );
- }
- Return super. shouldoverrideurlloading (view, URL );
- }
Copy code
The URL Decoding Method is omitted here. Downloadservice is used to download an MP3 file and receive the URL and music file name transmitted by downloadwebviewclient. The Code is as follows:
Java code:
- Public class downloadservice extends Service implements runnable {// implement the runable Interface
- Private string url_str; // path of the online song
- Private file download_file; // The downloaded file
- Private int total_read = 0; // The length of the downloaded object (in bytes)
- Private int readlength = 0; // the length of one-time download (in bytes)
- Private int music_length = 0; // The length of the music file (in bytes)
- Private Boolean flag = false; // whether to stop downloading. True is used to stop downloading.
- Private thread downthread; // download thread
- Private string musicname; // The downloaded file name.
- @ Override
- Public ibinder onbind (intent ){
- Return NULL;
- }
Second:
When meitu appreciates the Android app, it involves the Android app download function. The app itself is also relatively simple, that is, it calls the corresponding web page through webview Control for display. At first, I thought it was a normal file download implementation. I only needed a link and then clicked it to download it. However, when I put it on my mobile phone and tried it, I didn't even find any response when I clicked the download link, click OK on the normal page, and click other common links. It turns out that webview does not enable the file download function by default. If you want to implement the file download function, you need to set the webview downloadlistener to download the file by implementing your own downloadlistener. The procedure is as follows:
1. Set downloadlistener for webview:
Webview. setdownloadlistener (New mywebviewdownloadlistener ());
2. Implement the mywebviewdownloadlistener class as follows:
[Java]
View plaincopy
- Private class mywebviewdownloadlistener implements downloadlistener {
- @ Override
- Public void ondownloadstart (string URL, string useragent, string contentdisposition, string mimetype,
- Long contentlength ){
- Uri uri = URI. parse (URL );
- Intent intent = new intent (intent. action_view, Uri );
- Startactivity (intent );
- }
- }
This is just to call the built-in browser in the system for download, and there is no webview file download, But it basically meets our application scenarios.