can we call an android activity from Phonegap project? - javascript

I'm Developing an android application.
I've created some codings using HTML5 and javascript for android. And created a signature capturing application in Native JAva
I wanted to include the Native Signature capture coding in the HTML5 application. Because the signature capturing in the HTML5 is not working perfect for touch devices.
is it possible to call a native android activity from the HTML5 phonegap android application????

Yes, it is possible.
For doing the same you need to add cordova lib to your android project and moreover create a plugin in android by extending the Plugin class.
The overrided method named execute() is there that you can call from your javascripts.
Remember you need to define the path of the class containing execute folder to the config.xml.
Please visit below for complete guide.
Android Plugin

Any Java Native code call be called without using any plugin as following.
Follow The following Steps.
1) Replace the following code with your existing DroidGap Activity.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init(); // Calling this is necessary to make this work
appView.addJavascriptInterface(this, "MainActivity");
/* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */
super.loadUrl("file:///android_asset/www/index.html");
}
2 Add the custom function in current (this) activity as following.
public void customFunctionCalled() {
Log.e("Custom Function Called", "Custom Function Called");
}
3 Now call this function from your HTML/JavaScript code as following.
<script type="text/javascript">
function callNewActivity() {
window.MainActivity.customFunctionCalled();
}

Related

Android WebView push notification?

I need to send a notification (not necessarily a push notification) through an android webview. I saw that the Notification API was not compatible with Android Webview on MDN. The other APIs I saw seemed to be based off of window.notification.
Does anyone know of any API or JS that sends a notification through an android webview?
I saw this post from 6 months ago with essentially no activity except a vague mention of firebase. Would that be helpful?
Thanks for your answers!
I don't have enough "reputation" to post a comment but could that be useful to you?
Android Push Notification with WebView?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
Hopefuly you can make it work from one of those example
Read the documentation entry for Binding JavaScript code to Android code.
This allows you to use javascript to trigger the execution of android code.
First you have to register the Javascript Interface on android, so that you can trigger android code from javascript.
JAVA
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And define a method which does your action if the javascript is called. In this example show a toast.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
↑ You need to change that part to show your push notification instead. ↑
Then you can trigger the android code from javascript like this:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
I did not tried it myself. But I would try to create a javascript method which makes an ajax request to a server in certain intervals and checks if there are new notifications to send and if true then call the android code to show the message.
However, you will have to make sure to only show the notification once somehow... maybe set a cookie containing the notification ID and set it to true, so that the android code is not getting triggered again.
You would need to provide the notifications for example as a .json file in JSON format. You can upload that .json file to your webserver somewhere. Then pass the content to android and parse it accordingly.
One Simple workaround use JSInterafce - communicate webview to native.tutorial
In that JSInterface pass, the desired parameter for notification and then use android system notification API to generate the notification.

How to invoke JS function from Android WebView?

I'm trying to invoke JS method from Android level.
I created #JavascriptInterface. In the method of the WebViewClient class, I injected the JS script with the JS function which is calling Android interface method in Java (I injected it in WebViewClient #Override onPageFinished method)
Now from the JS level (by injecting it) I can run the Android method.
But I want also inform html page to reload by JS script - by changing URL hash (#fragment).
I mean, I want to call from the Android level the JS method of the html page,
specifically javascript: window.location.replace = ... or window.location.hash = ...
Is this even possible?

unable to execute javascript code in Android WebView from Service

I'm attempting to create and Android service that performs a task using JavaScript. I came across this post which describes how to run JavaScript code inside of a WebView within a Service using the WindowManager. I am able to create a WebView with an .html and .js file with no problem. It is once I try to pass data from the android .java service to the WebView that I run into an issue.
I have tried doing so in this fashion:
final WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
...
wv = new WebView(this);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("file:///android_asset/www/test.html");
windowManager.addView(wv, params); // params set using method from linked post above
wv.evaluateJavascript("console.log('hello');", null);
wv.loadUrl("javascript:console.log('blah')");
Neither the call to evaluateJavascript() nor loadUrl() appear to have any effect on the WebView (I access the console using the chrome developer tools).
I have tested that in test.html I can add a <script> tag and output text to the console with no issue.
I've also tried calling the functions before adding the view to no avail.
What kind of data you want to pass to Javascript? You could use the WebView.addJavascriptInterface() to "Plant" methods on the HTML document so you can call them from Javascript, invoke in native and return data back to Javascript. Will that help?
If you're trying to execute this from a Service, you'll need to post a Runnable on the UI thread. Read the doc for evaluateJavascript()
It explicitly says it must be called on the UI thread. I think you can just do:
wv.post(new Runnable(){
#Override public void run()
{
wv.evaluateJavascript();
}
});
Other than that, should you include tags?

what about JavaScript in android?

I am a android developer. I want to develop an android app but want to code in javascript for that. Is it possible using ReactNative?
http://www.reactnative.com/
You can defiantly write phone apps in JS, there are quite few options on the market:
On one hand there are hybrid apps, which written in HTML, CSS, JS and are built usually by something like cordova in order to communicate with phone API's. Some good frameworks that help you with the build and styling proccess are: Phonegap and Ionic.
And you have the react-native approach which basicly compiles the JS code to native phone components.
Both approaches let you reuse parts of your code in multiple platforms(Android, IOS).
While in hybrid apps you can reuse almost all of your code but just build for each platform. On react native you will have to code your views for each platform while your BL will stay the same if you written the code properly.
You have to use WebView for this. Then register the JavaScriptInterface on your webview. JavaScriptInterFace can be a inner class as shown below. This class will have a function that you can call from html page( via javaScript ) and inside this function you can write code to change activity.
public class JavascriptInterfaceActivity extends Activity {
/** Called when the activity is first created. */
WebView wv;
JavaScriptInterface JSInterface;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
// register class containing methods to be exposed to JavaScript
JSInterface = new JavaScriptInterface(this);
wv.addJavascriptInterface(JSInterface, "JSInterface");
wv.loadUrl("file:///android_asset/myPage.html");
}
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
public void changeActivity()
{
Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class);
startActivity(i);
finish();
}
}
}
Here is the html page
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
JSInterface.changeActivity();
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
Yes obviously you can use ReactNative to develop the android applications. You need to write the app code in javascript in this case.
You must use react native in this case. It has native components exposed to javascript. If you need some special component(That is not available in GitHub), you can write that native module in Android and IOS respectively. Also, it has a great community support.

#JavascriptInterface cannot be resolved

I'm working with Android to make a webpage interact with my app. As such, I've gone through the documentation and tutorials and ended up coming up with this site. In it, the developers list that you should include #JavascriptInterface before any function you wish to be accessible by the WebView and that without it, Jelly Bean won't recognize the function.
My problem is that when I put that in, I get an error saying:
#JavascriptInterface cannot be resolved to a type
Without it, my code compiles and works fine, but I want Jelly Bean compatibility. I'm currently working on Android 1.6 as a base, so does it just not have #JavascriptInterface? Is that a Jelly Bean specific thing, meaning I'll have to make a program specifically for Jelly Bean? Any help would be greatly appreciated. Here is my complete interface class:
import android.content.Context;
public class WebAppInterface{
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
//needed for jelly bean
#JavascriptInterface
public void turnOff() {
MyAlarmPopup.turnOff();
}
}
#JavascriptInterface is introduced in JellyBean.
You need to set the Project Build Target to API Level 17
From Eclipse IDE, go to
Project->Properties.
Select Android and set the target
The APK will work on older versions of android.
You need to add this:
import android.webkit.JavascriptInterface;
Check the sample here:
Steps for users of Android Studio:
Check that in build.gradle the minSdkVersion is 17 or above
Add import android.webkit.JavascriptInterface;

Categories

Resources