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.
Related
I'm creating an app that loads my website to a webview. I want to prevent that page reloads when I came back to app. If I change to other app or power off and came back the page shows without refresh, the problem occurs only it stays off for a few minutes. I think that the application change the state to stop or is destroyed and when I enter again is called the method oncreate.
I've tried to change android:launchMode to singleTop an singleTask without success, is the creation of a service the solution? Thanks in advance.
In order to prevent refreshing of WebView, you need to save and restore state.
This is done by overriding lifecycle methods onSaveInstanceState and onRestoreInstanceState
#Override
protected void onSaveInstanceState(Bundle outBundle)
{
super.onSaveInstanceState(outBundle);
webView.saveState(outBundle);
}
#Override
protected void onRestoreInstanceState(Bundle savedBundle)
{
super.onRestoreInstanceState(savedBundle);
webView.restoreState(savedBundle);
}
Furthermore, you can try to set cache on the WebView instance.
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl(url);
In HTML5WebView.java (pastebin link)(source) I have:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("TAG - BACK PRESSED IN WEB VIEW");
return super.onKeyDown(keyCode, event);
}
And in the activity that starts the web view I have:
#Override
public void onBackPressed() {
System.out.println("TAG - BACK PRESSED IN WEB PLAYER ACTIVITY");
super.onBackPressed();
}
Now when I'm playing an embedded youtube video normally (not fullscreen), both methods are called when I press the back button. When I put the video into fullscreen mode (using the youtube player fullscreen button), none of the methods are called. My only guess is, the back button is being consumed by the web view to undo the fullscreen action (but even that doesn't work).
I am trying to get the back button to immediatly kill the web view, even if there is a video in fullscreen mode.
See the problem in action here, had to post it externally because the gif > 2MB
If I was you I would delete the embed play of youtube video's or ask google that won't be seen as abuse. Because the embed play of youtube video's can be seen as a abuse of this:
Your app violates our Device and Network Abuse policy by downloading,
monetizing, or otherwise accessing YouTube videos in violation of the
YouTube Terms of Service or YouTube API Terms of Service.
More Info: https://play.google.com/about/privacy-security/device-network-abuse/ https://www.youtube.com/static?template=terms
For example, your app contains: YouTube background play functionality
This is a violation of the YouTube Terms of Service.
That is what I got in my experience because I also made a app where people could watch some YT embed video's but after some updates the app wouldn't be update becuase I got this error that it is abuse so I am warning you that's all.
I'm developing on a Galaxy S6 running Lollipop. I used Android Studio to create a ScrollView app using the template that comes with Android Studio. I only added the following Java code:
#Override
public void onBackPressed() {
Toast.makeText(this, "onBackPressed", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
#Override
protected void onPause() {
Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
super.onPause();
}
#Override
protected void onStop() {
Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
super.onStop();
}
#Override
protected void onDestroy() {
Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
When I tap Back on the device, all 4 toast messages come up in the sequence that they appear in the code. However, when I view the app in Application Manager, the Force Stop button is still enabled, indicating that the process is still active. To confirm that I also downloaded a third party app to view active processes and it shows mine in the list.
Is there something that has to addes so that when hitting the Back button on the device the process will die 100% and not be on that active processes list any longer?
This is expected behavior on Android.
Android makes no guarantees as to when it will kill your app when exiting, so I'm not sure why you think it's supposed to. It's more beneficial for Android to keep your app in memory as long as possible so that its's faster to resume.
Please this code in your OnBackPressed() method:
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
That way your app will be completely closed and removed from running apps.
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.
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?