How can I stop webChromeClient from opening files in the Android Browser - java

I am developing an Android App using WebView which loads a locally stored HTML5/JQuery Mobile app in the assets folder. The App gets content from a REST service on a domain I own. The content is a mixture of HTML content and downloadable files.
My problem lies when a user clicks on a button to download a file, a Browser window launches and the file is downloaded, which is great. However my App then loses focus and I have a blank browser on screen. I have done a bit of Googling and have read a few answers on Stack Overflow and have adapted my code to set a Download Listener, but this still loads the Browser. Please advise. My code is listed below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
webView.loadUrl("file:///android_asset/www/index.html");
}

Have you tried using the Android DownloadManager (please see http://developer.android.com/reference/android/app/DownloadManager.html) instead of starting a VIEW Intent? I think that you'd just need to enqueue a download from your DownloadListener.

Related

How can I get write access to the API?

When trying to set writing access in the scope I receive the application need permission
webView.loadUrl("https://stackoverflow.com/oauth/dialog?client_id=19361&redirect_uri=https://stackexchange.com/oauth/login_success&scope=no_expiry,private_info,read_inbox,write_access");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(url.contains("https://stackexchange.com/oauth/login_success#access_token=")){
String code=url.replace("https://stackexchange.com/oauth/login_success#access_token=","");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("accessToken",code);
startActivity(intent);
}
System.out.println(url);
super.onPageStarted(view, url, favicon);
}
});
You need to create any post on stackapps and set his url into stackapps post field
In order to get an access token with the write_access scope, you need a Stack Apps post.
After registering your application on StackApps and obtaining an API key and a client id, in the "Stack Apps Post" field you should enter the link to a question you have asked.
If you have already registered your app, go to "Manage your applications", select the respective application, scroll to the bottom, click "Edit this app", then enter your question link in the "Stack Apps Post" field. Then, submit your changes.
See My app has to be published first? But it's still under development.

Can not download the file from server in android web view

I want to download a file from my school web view app.
When I open my school website in chrome (android) it downloads my marks file, but in my Web view app it does not download. But then my Web view app can download course material inside Web view fine.
I have already implemented the download listener. In debugging i check that clicking on marks button it does not enter in to download block code?
You should set download listener to you web view just like this:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLengt[h) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
you can learn more about DownloadListener from here

error at WebChromeClient onProgressChanged event

I am creating a app for my blog. I used webview to load the blog site in the main activity. To make it more efficient I used a ProgressBar to do a loading animation when a url loading progress on going. Also add method when capture when another url is called, open it in the system web browser.
This is the code I have trouble with. The problem is when the program captured another url it opens a separate system browser but it keep lagging until I end the app from the Android Studio. Can somebody fix this or do there are any other option for this?
webv.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(!webv.getUrl().startsWith("https://lktechtronic.blogspot.com")){
webv.stopLoading();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(webv.getUrl())));
webv.goBack();
}else if(webv.getUrl().startsWith("https://lktechtronic.blogspot.com")){
loadinglay.setVisibility(View.VISIBLE);
if (newProgress == 100) {
loadinglay.setVisibility(View.GONE);
}
}
}
});
webv is the my WebView
loadinglay is the Layout with the ProgressBar
You may implement shouldOverrideUrlLoading and make your own WebView to load any redirect.

Android web view invoking and clicking element

I was working on a mobile app, the app plays music from some website using jplayer and changes from one song to another with out no user interaction, my first trail to build the app using phonegap which failed because HTML5 media tags are not allowed to start automatically on mobile devices because of bandwidth issue,the user must click some button to play the media so i am trying some thing new as below,
Create a native android app with a webView
Set the url of the webview url to a file in Assets folder
After the page is loaded and the media is ready to be played, programmatically click on the play button which is in the webview using java so that it can start playing
I have given the play button in the webview an id of playbtn
My java code
I have created a button in order to execute the clicking event of the playbtn.
final WebView wv;
Button playbtn;
wv = (WebView) findViewById(R.id.webView1);
playbtn = (Button) findViewById(R.id.button1);
wv.clearCache(true);
wv.clearHistory();
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.loadUrl("file:///android_asset/index.html");
playbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "test", Toast.LENGTH_SHORT).show();
// the toast shows up
wv.loadUrl("javascript:(function(){"+
"l=document.getElementById('playbtn');"+
"e=document.createEvent('HTMLEvents');"+
"e.initEvent('click',true,true);"+
"l.dispatchEvent(e);"+
"})()");
//nothing happens here
}
});
I don't thing using JavaScript interface will work, since there is no user interaction, is there any way to accomplish the clicking of the playbtn programmatically?
Update
Once playback has started clicking the button is now resuming the music.

How can I play videos of a specific youtube channel on my application without the help of browser or youtube application on android

Here I'm using this code,
public class WebTV extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webtv);
mWebView = (WebView) findViewById(R.id.webTV);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.youtube.com/banglanews24");
} }
but it'll play the video on the browser directly without showing the channel page on the webview, but instead of that if i use,
mWebView.loadUrl("http://www.youtube.com/banglanews24");
it'll show the channel page inside the webView but could not load the or render the videos.
I've also tried,
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/banglanews24")));
but none of this work for me.
Can any one give me a solution, through which i can play this youtube channel in side my application, like you tube application for android. Can any one help me out?

Categories