I have been getting an error while creating a WebView in android studio, the error says the API is deprecated. I'm adding the code of my file. I think it would be easy to find, but I have no experience in android so I'm not able to find the error.
I have read about the updates of the android / java but could not find this
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView MywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MywebView = (WebView) findViewById(R.id.view2);
WebSettings webSettings = MywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
MywebView.loadUrl("http://www.ynaps.com");
MywebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("tel:")) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}else{
return false;
}
}
});
}
#Override
public void onBackPressed() {
if (MywebView.canGoBack()) {
MywebView.goBack();
} else {
super.onBackPressed();
}
}
}
I should not get this error:
E:\APP\pro\sdddd-
dunia\app\src\main\java\com\business\ynaps_app\MainActivity.java:
uses or overrides a deprecated API.
Recompile with -Xlint:deprecation for details.
You must use shouldOverrideUrlLoading(WebView view, WebResourceRequest request) instead of shouldOverrideUrlLoading(WebView view, String url) which has been deprecated in API 24.
You can check the doc page here.
Error is fairly self explanatory. Your class have deprected method shouldOverrideUrlLoading. If you're not aware what does this mean either of the following link will help you understand.
Is it wrong to use Deprecated methods or classes in Java?
What does it mean for a method to be deprecated?
Some of the suggested solutions:
Essentially, you might just get over this error by adding jvm argument specified in error -Xlint:deprecation.
If that does does not help you can indicate JVM explicitly instructing method is deprecated with #Deprecated annotation.
Lower the jdk/jre (Jvm) to the version in which method is not yet deprecated
Correct solution is to update you code with removing usage of deprecated method and replace it with associated alternative method.
I think it has to do with this line:
webSettings.setJavaScriptEnabled(true);
The function 'setJavaScriptEnabled' seems to be deprecated.
Related
I am loading a webView in my app that displays some buttons, each button starts a route that is generated using google maps.
The problem is that when I click "Navigate in app" nothing happens.
I was having the URL_UNKOWN_SCHEME error but solved it by adding and #override
My code looks like this:
package com.example.listarehtgps2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webview);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.loadUrl("webViewURL replaced for privacy reasons");
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
if(url.contains("intent:")) {
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
startActivity(mapIntent);
return true;
}
view.loadUrl(url);
return true;
}
}
);
}
}
I can not find any solution to open the link in the google maps app.
I managed to solve the UNKWN_URL_SCHEME but this is frustrating now.
Thanks in advance!
The URL_UNKOWN_SCHEME is because it's not recognising the scheme part of your URI (the bit up to :), are your URIs simply starting with "intent:" ?
To open a map at a location, try switching to the geo scheme, so a URI something like "geo:0.01,0.01?q=London" (along ofc with changing your url.contains check to look for that). You could also insert a mapIntent.setPackage("com.google.android.apps.maps") before the call to startActivity(mapIntent) if you want Google maps to take priority over any other map application the user may have installed.
If however you want navigation, use a URI starting with google.navigation such as "google.navigation:London" or "google.navigation:0.01,0.01", equally for street view you can use a URI starting with google.streetview
This is my code:
package sai.datla.game;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class GametestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.clearSslPreferences();
webView.loadUrl("file:///android_asset/www/index.html");
}
}
but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.
By the way I am only twelve years old so please make the answers easy to understand for a child.
// Find view in layout
WebView wv = (WebView) findViewById(R.id.webView_tobe_loaded);
// Get settings
WebSettings wbset = wv.getSettings();
// Enable JavaScript
wbset.setJavaScriptEnabled(true);
// Set new client (to handle website in your app)
wv.setWebViewClient(new MyWebViewClient());
// Example of the URL
String url = "http://www.google.com";
// Load website
wv.loadUrl(url);
This code will help you to solve the problem.
It worked for me.
instead of loadUrl, try using the loadDataWithBaseURL method:
webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");
I've had an incredibly difficult time getting the below app to not show an error when the back button is pressed. All I want is the site to load a web page and the back button to perform as it normally does. Does anyone see anything wrong here? The LogCat shows no errors and with the emulator it doesn't break, but it always breaks on my tablet.
MyWebViewClient.java
package com.MySite;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
MySite.java
package com.mysite;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
public class MySite extends Activity {
WebView DCWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//init webview
WebView DCWebView = (WebView) findViewById(R.id.webview);
DCWebView.getSettings().setJavaScriptEnabled(true);
DCWebView.loadUrl("http://mobile.mysite.com");
//when a link is clicked, use the WebView instead of opening a new browser
DCWebView.setWebViewClient(new MyWebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && DCWebView.canGoBack()) {
DCWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
You are declaring a local variable called DCWebView inside of onCreate(). Change the line
WebView DCWebView = (WebView) findViewById(R.id.webview);
to
DCWebView = (WebView) findViewById(R.id.webview);
Then, when the back key is pressed, there will be a back stack (which is currently false when you call DCWebView.canGoBack(), since your global DCWebView was never initialized), the back button will be handled the way you are expecting.
What error are you getting when you hit back on a tablet? Have you tried overriding onBackPressed instead of onKeyDown?
I'm trying to use the Android tutorial to build an app that loads a web view to a mobile site that I built. The problem is with following the tutorial the startActivity function is undefined and the Android tutorial isn't helping. I've done Ctrl+Shift+O to verify all the proper modules are loaded.
package com.mysite;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Update
Ok, now my code reads:
package com.myapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MyApp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init webview
WebView DCWebView = (WebView) findViewById(R.id.webview);
WebSettings webViewSettings = DCWebView.getSettings();
//when a link is clicked, use the WebView instead of opening a new browser
DCWebView.setWebViewClient(new MyWebViewClient() {
#Override
public void launchExternalBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
//enable javascript
webViewSettings.setJavaScriptEnabled(true);
}
}
But I'm showing 2 errors:
Description Resource Path Location Type
The type new MyWebViewClient(){} must implement the inherited abstract method MyWebViewClient.launchExternalBrowser() DealClippings.java /MyApp/src/com/myapp line 21 Java Problem
The method launchExternalBrowser(String) of type new MyWebViewClient(){} must override or implement a supertype method MyApp.java /DealClippings/src/com/myapp line 23 Java Problem
There really is no startActivity method for WebViewClient. You can check the docs. You'll have to signal the Context (probably your Activity) to execute those lines of code instead. There are many possible approaches including adding listeners or simply calling an abstract method which you implement in an anonymous instance of this class when setting the WebViewClient of your WebView in your Activity.
For example:
public abstract class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
launchExternalBrowser(url);
return true;
}
public abstract void launchExternalBrowser(String url);
}
And then in your activity:
WebViewClient client = new MyWebViewClient() {
#Override
public void launchExternalBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
};
Although I'm not sure why you want this behavior exactly, but it should work more or less.
I don't know if there's much point answering your edit now, 18 months on, but it seems like this question gets a bit of traffic so I'll post this here for posterity.
From your errors, it sounds like you haven't provided an argument to the abstract method in the abstract class definition of MyWebViewClient. That is, you have this:
public abstract void launchExternalBrowser();
when you should have this:
public abstract void launchExternalBrowser(String url);
The cause of the error is that Java treats two methods with the same name but different arguments as two distinct methods. So launchExternalBrowser(String) is a different method to launchExternalBrowser().
Hope this helps someone!
To answer original question before your edit..
Had the same problem, and figured out that the MyWebViewClient is meant to be an inner class inside the activity.
package com.myapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyApp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init webview
WebView DCWebView = (WebView) findViewById(R.id.webview);
WebSettings webViewSettings = DCWebView.getSettings();
//when a link is clicked, use the WebView instead of opening a new browser
DCWebView.setWebViewClient(new MyWebViewClient());
//enable javascript
webViewSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
I have the following code that is working but when I hit the hardware back button the app crashes,
package com.fnesse.tmpmobile;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
#SuppressWarnings("unused")
public class TmpmobileActivity extends Activity
{
WebView webView;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(0x00000000);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
//Error Code Here
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.themorningtonpeninsula.com");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I found somewhere that I needed to add,
WebView webView;
Above the onCeate method, which allowed webView to resolve. So now I get no errors but it crashes and the app runs on my phone but crashes when I hit the hardware back button.
Does anyone know why that would be happening?
Cheers,
Mike.
I'll try to do my magic guest on the error message. I think, this error is caused by the way to initialize webView variable. You see, on the class level, you declare:
WebView webView;
And then, on onCreate() you initialize webView and also redeclare it again:
WebView webView = (WebView) findViewById(R.id.webview);
But, this redeclare step resulted in class level webView to be uninitialized.
To fix it, try to change this line:
WebView webView = (WebView) findViewById(R.id.webview);
Into:
webView = (WebView) findViewById(R.id.webview);
And you should now have class level webView initialized and ready for use in whatever method you have.
Here's few resources explaining Java variable scope:
http://www.java-made-easy.com/variable-scope.html
http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm
You are creating a new Webview in your onCreate() method instead of the Class field Webview. Change
WebView webView = (WebView) findViewById(R.id.webview);
into:
webView = (WebView) findViewById(R.id.webview);