Android: "complete action using..." for custom WebView running ACTION_VIEW intent on an mp3 url
If you open an mp3 url in the Android's browser, you get a "Complete action using..." dialog window with two options: "Music player", or "Browser". If you choose "Browser" it will download the mp3 to the device.
I'm trying to achieve the same from within my application. I use a WebView with setWebViewClient to a WebViewClient class that has a shouldOverrideUrlLoading method (like in all of the examples). The method checks for an "mp3" extension and startActivity(intent) with a ACTION_VIEW intent on the mp3 url.
@Override
public boolean shouldOverrideUrlLoading(WebView view, St开发者_JS百科ring url) {
String lowcaseurl = url.toLowerCase();
if (lowcaseurl.endsWith(".mp3")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
else {
view.loadUrl(url);
}
return super.shouldOverrideUrlLoading(view, url)
}
This does show the "Complete action using" dialog window - but, if I choose "Browser" then the browser opens but doesn't get the url! it just opens the browser and goes to the default "homepage url" (which is google.com in this case)...
Any help will be appreciated.
Couldn't find a direct solution, but used a workaround. I've figured out how to either force the music player to load the url (without giving you a choice of opening it via the browser), or initiate a direct download of the file.
To force the music player to load a url:
//...considering url contains the url string...
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "audio/*");
startActivity(intent);
To force the initiation of a download:
You need to implement this technique on the server-side... but with a few exceptions:
The content-type header should actually contain the right mime-type of the file (and not "application/octet-stream"). Also it is best to also send a "content-length" header with the file size - otherwise there might be problems with the download.
If using aspx: Response.TransmitFile is better than Response.WriteFile - and in that case Response.Buffer should be set to false.
You can use:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity( Intent.createChooser(intent, "Open...") );
with this you force the dialog "Complete ..."
精彩评论