Am working on app which i recently implemented a background service with help from the following: https://github.com/Red-Folder/Cordova-Plugin-BackgroundService/
Everything works fine and the service runs in the background when the phone is restarted.
But in the background service which executes a Java method Every 5mins 'DoWork' Line 20 https://github.com/Red-Folder/Cordova-Plugin-BackgroundService/blob/master/2.2.0/MyService.java
package com.yournamespace.yourappname;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;
public class MyService extends BackgroundService {
private final static String TAG = MyService.class.getSimpleName();
private String mHelloTo = "World";
#Override
protected JSONObject doWork() {
JSONObject result = new JSONObject();
try {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String now = df.format(new Date(System.currentTimeMillis()));
String msg = "Hello " + this.mHelloTo + " - its currently " + now;
result.put("Message", msg);
Log.d(TAG, msg);
} catch (JSONException e) {
}
return result;
}
#Override
protected JSONObject getConfig() {
JSONObject result = new JSONObject();
try {
result.put("HelloTo", this.mHelloTo);
} catch (JSONException e) {
}
return result;
}
#Override
protected void setConfig(JSONObject config) {
try {
if (config.has("HelloTo"))
this.mHelloTo = config.getString("HelloTo");
} catch (JSONException e) {
}
}
#Override
protected JSONObject initialiseLatestResult() {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onTimerEnabled() {
// TODO Auto-generated method stub
}
#Override
protected void onTimerDisabled() {
// TODO Auto-generated method stub
}
}
I would like to call a JavaScript function also from that method.
The Javascript Function does the following:
- Gets all the device contacts
- Get the device GeoLocation
- Get the device IMEI and phonenumber
and post to an external server.
I would like to know if that is possible i.e calling the javascript function from Java.
Note: I do not know much about Java so detailed explanation will be appreciated.
Thanks in advance !
Related
I'm working with a specific API that returns a class as a string to me. I need to insert a string at a certain block of the string that is given to me. So basically a function that takes the whole string, and appends the string I want to add to it after a specific block.
The string passed to me is a java class, and I want to basically enter my own function at the end of it after all of the existing functions. Incase you are confused.. I don't have access to the java file, this is the only way to modify the file when you are using config plugins in expo react native.
I believe some sort of regex is supposed to be used to get this result ? but really I have no idea how to target the specific part of the string.
The string I want to add:
'#Override\nprotected List getPackages() {\nreturn Arrays.asList(\nnew MainReactPackage(), // <---- add comma\nnew RNFSPackage() // <---------- add package\n);\n}'
The string that is passed to me
import expo.modules.updates.UpdatesDevLauncherController;
import expo.modules.devlauncher.DevLauncherController;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import androidx.annotation.NonNull;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import expo.modules.ApplicationLifecycleDispatcher;
import expo.modules.ReactNativeHostWrapper;
import com.facebook.react.bridge.JSIModulePackage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHostWrapper(
this,
new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return DevLauncherController.getInstance().getUseDeveloperSupport();
}
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return packages;
}
#Override
protected String getJSMainModuleName() {
return "index";
}
#Override
protected JSIModulePackage getJSIModulePackage() {
return new ReanimatedJSIModulePackage();
}
});
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
DevLauncherController.initialize(this, getReactNativeHost());
if (BuildConfig.DEBUG) {
DevLauncherController.getInstance().setUpdatesInterface(UpdatesDevLauncherController.initialize(this));
}
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
ApplicationLifecycleDispatcher.onApplicationCreate(this);
}
#Override
public void onConfigurationChanged(#NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig);
}
<--- I WANT TO INSERT MY STRING HERE
/**
* Loads Flipper in React Native templates. Call this in the onCreate method with something like
* initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
*
* #param context
* #param reactInstanceManager
*/
private static void initializeFlipper(
Context context, ReactInstanceManager reactInstanceManager) {
if (BuildConfig.DEBUG) {
try {
/*
We use reflection here to pick up the class that initializes Flipper,
since Flipper library is not available in release mode
*/
Class<?> aClass = Class.forName("com.haibert.GitTest.ReactNativeFlipper");
aClass
.getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
.invoke(null, context, reactInstanceManager);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
const addToMainApp = (content) => {
const regexpPackagingOptions = /\s*?(?=\/\*\*\n \* Loads Flipper)/
const insertLocation = content.match(regexpPackagingOptions)
const newContent =
content.substring(0, insertLocation.index) +
'//INSERTED \n#Override\nprotected List<ReactPackage> getPackages() {\nreturn Arrays.<ReactPackage>asList(\nnew MainReactPackage(), // <---- add comma\nnew RNFSPackage() // <---------- add package\n);\n}' +
content.substring(insertLocation.index, content.length)
return newContent
}
I'd like to import a module written natively (java, Android) into my React Native sources, in JS.
To access your functionality implemented in java you have to create a bridge. You can see the most recent instructions in the RN documentation site*.
The steps, assuming React Native 0.61, for a hello world, to be implemented in the android project inside the react native app directory (android directory):
1) First you create a simple POJO class to be returned to the react native context:
class MyData{
private int timeSpentSleeping;
public int getTimeSpentSleeping() {
return timeSpentSleeping;
}
public void setTimeSpentSleeping(int timeSpentSleeping) {
this.timeSpentSleeping = timeSpentSleeping;
}
#NonNull
#Override
public String toString() {
Gson gson = new Gson();
String json = gson.toJson(this);
return json;
}
static MyData build(final int timeSpentSleeping){
MyData newInstance = new MyData();
newInstance.timeSpentSleeping = timeSpentSleeping;
return newInstance;
}
}
And the react native module that do something and return objects of this class as javascript Promises:
public class HelloPromiseModule extends ReactContextBaseJavaModule {
public HelloPromiseModule(#NonNull ReactApplicationContext reactContext) {
super(reactContext);
}
#NonNull
#Override
public String getName() {
return "HelloPromise";
}
#ReactMethod
public void foobar(Promise promise){
Random r = new Random();
final int timeToSleep = r.nextInt(1000);
runThreadAndCallPromiseToJavascript(timeToSleep, promise);
}
//Cria um thread pra executar algo em paralelo
private void runThreadAndCallPromiseToJavascript(final int timeToSleep,final Promise promise){
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(timeToSleep);
MyData result = MyData.build(timeToSleep);
promise.resolve(result.toString());
} catch (InterruptedException e) {
e.printStackTrace();
promise.reject(e);
}
}
});
t.run();
}
}
Now, we create the React Native Package (that is different from java packages):
public class HelloWorldPackage implements ReactPackage{
#NonNull
#Override
public List<NativeModule> createNativeModules(#NonNull ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
new HelloPromiseModule(reactContext));
}
#NonNull
#Override
public List<ViewManager> createViewManagers(#NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
The last step in the android version of your react native app is to register your HelloWorldPackage:
In the MainApplication.java inside your android project, inside the getPackages(), in the list of packages (new PackageList(this)...):
packages.add(new HelloWorldPackage());
Something like that:
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new HelloWorldPackage());
return packages;
}
Now, to get your native class in the javascript world:
import {
NativeModules,
} from 'react-native';
...
const {HelloPromise} = NativeModules;
Your native class is accessible from the variable HelloPromise.
You can get the result of HelloPromise.foobar() with something like this, in the react native side of your code:
async function handleHelloPromisesPress() {
let result = await HelloPromise.foobar();
console.log(result);
}
You may notice that 'result' is a json whose structure is equal to the POJO class we created in the beginning.
I'm trying to write a Java API that will be called from server side JavaScript running under the JDK 7 JS engine. The idea is to give consumers the ability to write JS code like below, registering a callback that is later executed by another call to a Java method:
myJavaObj.registerCallback(function (param) {
// do stuff here
});
myJavaObj.methodThatTriggersCallback();
Here is some test code I'm using:
import javax.script.Invocable;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JSTestClient {
private String successCallback;
public void handleSuccess(String successCallback) {
this.successCallback = successCallback;
}
public void doStuff() throws ScriptException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
Invocable engine = (Invocable) manager.getEngineByName("JavaScript");
engine.invokeFunction(successCallback, "TEST SUCCESS");
}
}
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String js =
"var client = new Packages.JSTestClient();\n" +
"client.handleSuccess(function (response) {\n" +
" java.lang.System.out.println(response);\n" +
"});\n" +
"client.doStuff();";
try {
engine.eval(js); // Expecting this to output "TEST SUCCESS"
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
But when I run this I get a java.lang.NoSuchMethodException because it is interpreting the string: function (response) {
java.lang.System.out.println(response);
} as a function name. Is there a way to create a callback like this or do I need to use some other convention?
Your handleSuccess method takes a String as the argument, but you're calling it with a JavaScript function object. You need to change it to accept a Function.
Since you're using Java 7, you are using the modified Mozilla Rhino engine, where the implementation classes were moved from package org.mozilla.javascript to sun.org.mozilla.javascript.internal.
To get your code running, replace the JSTestClient with this:
import sun.org.mozilla.javascript.internal.Context;
import sun.org.mozilla.javascript.internal.Function;
public class JSTestClient {
private Function successCallback;
public void handleSuccess(Function successCallback) {
this.successCallback = successCallback;
}
public void doStuff() {
this.successCallback.call(Context.getCurrentContext(), null, null,
new Object[] { "TEST SUCCESS" });
}
}
Be aware that Java 8 changed to the Nashorn engine, and the code for interacting with Nashorn is entirely different.
I have a bean that implements JSObject ( & Map ) interface as shown below. I have removed some overridden methods to make it easy to read.
package test.nashorn;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import jdk.nashorn.api.scripting.JSObject;
public class JSBean implements JSObject, Map<String,Object>{
/**
* The current values for this object.
*/
private HashMap<String, Object> values = new HashMap<>();
#Override
public String toString() {
System.out.println("ToString");
Set<Entry<String,Object>> entries = values.entrySet();
StringBuilder sb = new StringBuilder();
for(Entry<String,Object> entry:entries){
sb.append(entry.getKey()+ " "+(String)entry.getValue());
}
System.out.println("Completed ToString");
return sb.toString();
}
#Override
public boolean hasMember(String name) {
return has(name);
}
// get the value of that named property
#Override
public Object getMember(String name) {
return get(name);
}
// get the value of that named property
#Override
public void setMember(String name,Object value) {
put(name,value);
}
public Object get(String name) {
System.out.println("JAVA Get is called."+name);
System.out.println("Called for this"+name+" and returned"
+":"+values.get(name));
return values.get(name);
}
#Override
public Object put(String name, Object value) {
System.out.println("JAVA Put is called. Input name: " + name + "\n Input values: " + value);
return values.put(name, value);
}
public boolean has(String name) {
System.out.println("JAVA Has is called. Input name: " + name);
return values.containsKey(name);
}
public JSBean() {
// TODO Auto-generated constructor stub
}
#Override
public Object call(Object arg0, Object... arg1) {
// TODO Auto-generated method stub
return null;
}
#Override
public Object eval(String arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public String getClassName() {
// TODO Auto-generated method stub
return null;
}
#Override
public Object getSlot(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean hasSlot(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isArray() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isFunction() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isInstance(Object arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isInstanceOf(Object arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isStrictFunction() {
// TODO Auto-generated method stub
return false;
}
}
When i run the test shown below
#Test
public void testDefaultValMethod(){
JSBean bean = new JSBean();
bean.setMember("hello", " Sport ");
//Add stuff to engine.
engine.put("jsBean", bean);
String source = "(function(){\n"
+ "print(jsBean);"
+ "} )();";
Object obj=null;
try {
obj = engine.eval(source);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Returned : " + String.valueOf(obj));
}
I see the below error in the console. Ideally Nashorn should have directly called the toString() method of the bean to get the String implementation. Not sure what is going wrong here. I did try adding a call to 'toString()' explicitly in the getMember() method call but that did not fix the problem.
JAVA Put is called. Input name: hello
Input values: Sport
JAVA Get is called.toString
Called for thistoString and returned:null
JAVA Get is called.valueOf
Called for thisvalueOf and returned:null
javax.script.ScriptException: TypeError: cannot.get.default.string in <eval> at line number 2
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:467)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:451)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:403)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:399)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at test.nashorn.NashornTest.testDefaultValMethod(NashornTest.java:386)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: <eval>:2 TypeError: cannot.get.default.string
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:514)
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:480)
at jdk.nashorn.internal.runtime.JSType.toStringImpl(JSType.java:1391)
at jdk.nashorn.internal.runtime.JSType.toString(JSType.java:589)
at jdk.nashorn.internal.objects.Global.printImpl(Global.java:2782)
at jdk.nashorn.internal.objects.Global.println(Global.java:1497)
at jdk.nashorn.internal.scripts.Script$Recompilation$1$11$\^eval\_.L:1(<eval>:2)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:640)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:228)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:446)
... 29 more
Caused by: java.lang.UnsupportedOperationException: cannot.get.default.string
at jdk.nashorn.api.scripting.DefaultValueImpl.getDefaultValue(DefaultValueImpl.java:53)
at jdk.nashorn.api.scripting.AbstractJSObject.getDefaultValue(AbstractJSObject.java:289)
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:512)
... 40 more
Returned : null
print or "toString" conversions from scripts call "toString" method on the script object. Any property access (including function valued property) on a JSObject is routed to getMember method. So, to make "valueOf" or "toString" you've to implement appropriate getMember in your JSObject subtype.
Example:
import jdk.nashorn.api.scripting.*;
import javax.script.*;
public class Main {
static class MyJSObject extends AbstractJSObject {
#Override
public Object getMember(String name) {
if (name.equals("toString")) {
// return a "function" object for "toString" property
return new AbstractJSObject() {
#Override
public boolean isFunction() {
return true;
}
#Override
public Object call(Object self, Object...args) {
return self.toString();
}
};
}
return null; // other properties here
}
#Override
public String toString() {
return "my js object";
}
}
public static void main(String[] a) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
e.put("myObj", new MyJSObject());
e.eval("print(myObj)");
}
}
Alternatively, you can also override
Object getDefaultValue(final Class<?> hint) throws UnsupportedOperationException
method in your AbstractJSObject subclass.
import jdk.nashorn.api.scripting.*;
import javax.script.*;
public class Main2 {
static class MyJSObject extends AbstractJSObject {
#Override
public Object getDefaultValue(Class<?> hint) {
if (hint == String.class) {
return toString();
}
throw new UnsupportedOperationException("no conversion for " + hint);
}
#Override
public String toString() {
return "my js object";
}
}
public static void main(String[] a) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
e.put("myObj", new MyJSObject());
e.eval("print(myObj)");
}
}
Im trying to send notifications to a Titanium App from AeroGear. After getting the token, how can subscribe to the channel?
Obteining the token:
var CloudPush = require('ti.cloudpush');
var deviceToken = null;
CloudPush.retrieveDeviceToken({
success: deviceTokenSuccess,
error: deviceTokenError
});
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
CloudPush.addEventListener('callback', function (evt) {
alert("Notification received: " + evt.payload);
});
This is the example code for native Androiod:
package com.push.pushapplication;
import java.net.URI;
import java.net.URISyntaxException;
import org.jboss.aerogear.android.unifiedpush.PushConfig;
import org.jboss.aerogear.android.unifiedpush.PushRegistrar;
import org.jboss.aerogear.android.unifiedpush.Registrations;
import android.app.Application;
public class PushApplication extends Application {
private final String VARIANT_ID = "variant_id";
private final String SECRET = "secret";
private final String GCM_SENDER_ID = "1";
private final String UNIFIED_PUSH_URL = "URL";
private PushRegistrar registration;
#Override
public void onCreate() {
super.onCreate();
Registrations registrations = new Registrations();
try {
PushConfig config = new PushConfig(new URI(UNIFIED_PUSH_URL), GCM_SENDER_ID);
config.setVariantID(VARIANT_ID);
config.setSecret(SECRET);
config.setAlias(MY_ALIAS);
registration = registrations.push("unifiedpush", config);
registration.register(getApplicationContext(), new Callback() {
private static final long serialVersionUID = 1L;
#Override
public void onSuccess(Void ignore) {
Toast.makeText(MainActivity.this, "Registration Succeeded!",
Toast.LENGTH_LONG).show();
}
#Override
public void onFailure(Exception exception) {
Log.e("MainActivity", exception.getMessage(), exception);
}
});
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
Really lost here, any help would be appreciated!
You need to make wrapper around AeroGear native library as titanium module. However, it may be difficult if you didn't it before.
The titanium module that you need to get this working has been made by "Mads" and you can find it here: https://github.com/Napp/AeroGear-Push-Titanium