JS can call Android and get a value back!

This commit is contained in:
Bradlee Speice 2014-09-01 14:32:13 -04:00
parent e20e42b7bd
commit ae43667f2f
4 changed files with 15 additions and 0 deletions

View File

@ -67,6 +67,7 @@ dependencies {
compile 'com.netflix.rxjava:rxjava-android:+' compile 'com.netflix.rxjava:rxjava-android:+'
compile 'com.android.support:appcompat-v7:20.+' compile 'com.android.support:appcompat-v7:20.+'
compile 'org.apache.commons:commons-lang3:+' compile 'org.apache.commons:commons-lang3:+'
compile 'com.google.code.gson:gson:+'
androidTestCompile 'com.jayway.awaitility:awaitility:+' androidTestCompile 'com.jayway.awaitility:awaitility:+'
androidTestCompile 'org.mockito:mockito-core:+' androidTestCompile 'org.mockito:mockito-core:+'

View File

@ -31,6 +31,8 @@ window.appendVerse = function(text) {
return scope.$apply(); return scope.$apply();
}; };
console.log(Android.testReturn("Good morning."));
/* /*
Future reference: Get the controller scope like so: Future reference: Get the controller scope like so:

View File

@ -23,6 +23,8 @@ window.appendVerse = (text) ->
# Since we're calling outside of angular, we need to manually apply # Since we're calling outside of angular, we need to manually apply
scope.$apply() scope.$apply()
console.log Android.testReturn "Good morning."
### ###
Future reference: Get the controller scope like so: Future reference: Get the controller scope like so:
angular.element($("<controller-element>")).scope().<function> angular.element($("<controller-element>")).scope().<function>

View File

@ -7,6 +7,7 @@ import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
@ -125,6 +126,15 @@ public class BookFragment extends BaseFragment {
} }
}); });
// We can receive and return only primitives and Strings. Still means we can use JSON :)
mainContent.addJavascriptInterface(new Object() {
@JavascriptInterface
@SuppressWarnings("unused")
public String testReturn(String echo) {
return echo;
}
}, "Android");
// TODO: Remove remote debugging when ready - or should this be removed? // TODO: Remove remote debugging when ready - or should this be removed?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true); WebView.setWebContentsDebuggingEnabled(true);