Android call JS from Native Code - java

I'm trying to call JS functions from Android native code, but they don't seem to be working. I've tried many solutions but to no solution. Here is my code:
public class MainActivity extends Activity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyCustomWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("http://google.com");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:alert('shadow');");
Toast.makeText(getApplicationContext(), "DONE", Toast.LENGTH_LONG).show();
}
}
}
Please help.
Thanks

You can't do alert() with Android's WebView. If you want to show alert dialogs, you need to handle it in your Activity code or use a WebChromeClient. Per the docs:
Creating and setting a WebChromeClient subclass. This class is called
when something that might impact a browser UI happens, for instance,
progress updates and JavaScript alerts are sent here (see Debugging
Tasks).
See: https://developer.android.com/reference/android/webkit/WebView.html
With a WebView you can still write a JavaScript function that sets the value of a textbox to some string. If you need specific code to help you with this, let me know. If you've gotten this far though, you probably can handle it on your own is my guess.
Edit 1
First create a method in JavaScript called sendValueToAndroid()
myWebView.loadUrl("javascript:sendValueToAndroid()");
In the JavaScript method, call an exposed method in your Android code.
function sendValueToAndroid()
{
val divValue = ...
Android.sendValueToAndroid(divValue);
}
Expose a method in the Android app in any object of your choosing.
#JavascriptInterface
public String sendValueToAndroid(String val)
{
//do something in your app
}
Basically, what you're doing is telling the WebView to invoke a JavaScript method which invokes a callback method in your own app.

Related

Webview activity not showing anything on second load

I have this weird problem when showing an activity which consists (only) out of a webview.
The first time I start this Activity B by clicking on something in activity A, B is properly loaded and functional. I press the return button, go back to A, then click on the same button to go to B and my webview doesn't show anything. Debugged it tons of times and nothing seems out of the ordinary. There are also no crashes in the stack trace log... This problem by the wway occurs always in debug mode and only sometimes in run mode.
Here is the code for the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin);
myItem = (Item) getIntent().getSerializableExtra("Item");
initView();
initHandlers();
}
private void initView()
{
webView = findViewById(R.id.webview_spin);
//TODO : webViewClient vs webChromeClient ?
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String json = new Gson().toJson(myItem);
webView.loadUrl("file:///android_asset/item.html?item="+json);
}
private void initHandlers()
{
OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
#Override
public void onOrientationChanged(int orientation) {
webView.reload();
}
};
orientationEventListener.enable();
}
FYI this problem occured before setting the orientationlistener.
That looks like somehow the render process of the WebView is failing. You could store the WebViewClient you created on a variable and check the result of its method onRenderProcessGone() to check if it is rendering the second time you load the activity. Here you have all the WebViewClient methods: https://developer.android.com/reference/android/webkit/WebViewClient

How to create a wrapper object for a webview

so I have a working webview that is simple, but most of the work is done in the main activity. I am trying to break it out to its own little class so I can load other classes into the main activity on the fly. I am stuck on how to do this. I have tried this a few times and been debugging it but I just dont know if I am using the right findByValue(R.id.webView); thing here. it keeps giving me a
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
issue. I have tried passing the reference in as a parameter and also hard coded it. I am missing something but dont know what. There is a WebView in my activity_main that is called wevView that is what I am trying to find out where i'm did mistake.
Thanks for any help with this.
So this is my webview class:
public class NewWebView {
//variables need for webview
private final String myPage = "http://www.google.com";
WebView myWebView = null;
//consturctor
public NewWebView(){}
//This method creates the WebView
public void createWebView(WebView webView)
{
//WebView code
//Find the Webview in the Activity.xml file
//myWebView = webView;
myWebView = (WebView)myWebView.findViewById(R.id.webview);
// myWebView = (android.webkit.WebView)findViewById(R.id.webview);
//Use the custom WebViewClient for internal app browsing
myWebView.setWebViewClient(new MyWebViewClient());
//now for enabling the settings
WebSettings webSettings = myWebView.getSettings();
//Enable JavaScript
webSettings.setJavaScriptEnabled(true);
//Enable andriod zoom features
webSettings.setBuiltInZoomControls(true);
//Loading the Url
myWebView.loadUrl(myPage);
}
//Methods Used for WebViewer
//custom WebViewClent needed for internal app navigatiion
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverRideUrlLoading(android.webkit.WebView view, String url)
{
if(Uri.parse(url).getHost().equals(myPage))
{
//this is Apivita remain in the APP
return false;
}
/*
//if it is not in my site redirect it to mobile browers
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
*/
return true;
}
}
//Enable the use of the system back button for navigation
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//if the back button was pushed
if((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
//if not
return false;
}
}
and here is the main activity
public class MainActivity extends AppCompatActivity {
WebView myWebView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create the webview
myWebView = (android.webkit.WebView)findViewById(R.id.webview);
NewWebView apWebView = new NewWebView();
apWebView.createWebView(myWebView);
}
}
It looks like you are finding your webview in the main activity and then passing it into your class. In your custom class, you're then trying to find another view, which you technically already have. So its trying to find a webview, inside of the webview. It obviously can't find this, so it's null.
In theory, you should be able to just use myWebView = webView;, however, you need to make sure that it is finding it in the first place.

Android listener for webpage to start loading?

I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView

How to setup Android WebView to make it work identically Android browser?

What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview

Android - How to run intent from JavaScript

I am new to Android and Java. I have constructed an app using HTML/Javascript that is working great.
I now need to create an activity that launches the email client, fills in subject and body, and (the tough part) adds a file attachment. I have not been able to do this from within JavaScript, mailto: will not attach the file.
So I need to accomplish this through Java and execute it from JavaScript. I think this can be done by using addJavaScriptInterface but I cannot find any detailed documentation or examples to go off of.
How could I do this?
Here is what I have so far after reading the documentation:
2nd update to code:
MainActivity.java
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty( "splashscreen", R.drawable.splash );
super.loadUrl("file:///android_asset/www/index.html", 1000);
WebView mWebView;
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(), "Android");
}
}
JavaScriptInterface.java
public class JavaScriptInterface {
public void doEmail(){
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/html");
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://test co.html"));
startActivity(Intent.createChooser(sendIntent, "Send email..."));
}
}
Then I would reference the intent through JavaScript by using Android.doEmail().
With the above code I am getting 2 errors in Eclipse
1. The method startActivity(Intent) is undefined for the type - JavaScriptInterface
2. webview cannot be resolved or is not a field - MainActivity
What am I doing wrong?
This documentation tells you exactly how to do it.
It looks like there are three main steps:
Create your 'interface' class in Android
Add an instance of this 'interface' to the WebView you are using.
Call the interface from your JavaScript.
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty( "splashscreen", R.drawable.splash );
JavaScriptInterface jsi = new JavaScriptInterface(this, appView);
appView.addJavascriptInterface(jsi, "Android");
super.loadUrl("file:///android_asset/www/index.html", 1000);
}
}
and
public class JavaScriptInterface {
private WebView mAppView;
private DroidGap mGap
public JavaScriptInterface (DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public void doEmail(){
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/html");
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://test co.html"));
startActivity(Intent.createChooser(sendIntent, "Send email..."));
}
}
Using addJavaScriptInterface will extend the DOM inside the embedded browser, and allow JS to access a Java object, which is exactly what you want.
There are too many steps to outline here, that have already been documented. This link has a good overview.
I used WebIntents from Boris Smus (http://smus.com/android-phonegap-plugins) and it works like a charm. You can also peruse his code a little to understand better the approach he took with plugins.
NOTE: you do need to update the code provided as is a little (see comments) and the plugin architecture has changed a little.

Categories