I am currently making a Cordova plugin that will have call a method which starts a tween animation on the main activity.
cordova.getActivity().customMethod();
Does not work because the plugin does not know whether the method is implemented or not. How do I do this?
Thanks!
Edit: To clarify my question a little bit better: How do I make a Cordova Plugin start an Android view-animation in the current activity?
Did you add your plugin name to plugins.xml or config.xml(as from version 2.0.0)?
like -->
<plugin name="Your_plugin_name" value="packagename.Your_plugin_name"/>
Edit:
As you are simply calling a method in your activity,I wouldn't want to write a plugin for that...but you can do that just by getting a reference from that activity in the plugin and calling the method.After all the Plugin is implemented in native Java code.But if you have implemented CordovaWebView,then instead I would rather call it directly from the javascript.
In onCreate:
cordovaWebView.getSettings().setJavaScriptEnabled(true);
cordovaWebView.addJavascriptInterface(this, "reference");
In you activity:
public void animate()
{
//do animation
}
In JS :
reference.animate();
Do as you may like.
Related
I have been trying to call a Java method in unity. Not working for me, even the simplest example from the docs
using System.Collections;
using UnityEngine;
public class ExampleClass : MonoBehaviour {
void Start () {
AndroidJavaObject jo = new AndroidJavaObject ("java.lang.String", "some string");
int hash = jo.Call<int> ("hashCode");
Debug.Log ("hash=" + hash);
}
}
Unity console prints hash=0, which is not the hash code for provided String. Even if I change and use java.lang.StringInvalidClass as class name, unity still reports same result to the console without notifying errors. I can even try to call toString, toStringa, toInvalid2 and they always return empty string without showing errors.
This is a brand new 2d project with only script displayed above, attached to camara object. I am using Ubuntu, Unity 2019.4 and project platform is Android.
Thanks for the assistance.
Answering myself after some time working with unity.
All examples in the web and unity documentation doesn't mention it, which is weird since it is something simple to mention and about confusions: code needs to run as an android application. Editor or even unity remote does not work, in order to use AndroidJavaObject and related classes, your code needs to run as an android application installed in the phone.
After adding compile 'org.webrtc:google-webrtc:1.0.+' to my build.gradle file I try to init PeerConnectionFactory, but this class has no any useful methods.
What am I doing wrong?
UPDATE:
enter image description here
The last version org.webrtc:google-webrtc:1.0.21217
You can init by following codes
PeerConnectionFactory.InitializationOptions.Builder optionBuilder =
PeerConnectionFactory.InitializationOptions.builder(/* Put context here */);
optionBuilder.setEnableInternalTracer(true);
optionBuilder.setFieldTrials("WebRTC-FlexFEC-03/Enabled/");
optionBuilder.setEnableVideoHwAcceleration(true);
PeerConnectionFactory.initialize(optionBuilder.createInitializationOptions());
First, try to use an specific version like: compile 'org.webrtc:google-webrtc:1.0.20198'
And then make sure you rebuild your project (not only refresh gradle, since it might not be enough for the autocomplete to work).
In your screenshot, it looks like you are trying to autocomplete outside of any method. Since Android Studio tries to only show you valid stuff, it won't display the other methods unless you write it on a valid context (i.e.: inside of some method's implementation).
I have some new questions in today's citymaps development.
In the Android studio,if I develop the code for citymap, there are always no logs showing but for others that does not happen. Why?
According to the citymaps official website, to create a map instance with CitymapsMapFragment, but in the sample project which citymaps provides, it uses SupportCitymapsMpaFragment ,What is the difference between them?
When the map is loading complete, is it automatically positioning to the current position or some other default position? Where is it?
If I open the GPS location,I can locate to the current position and show a blue arrow quickly, but too much power consumption,are there any other location way like network or base station location?
Code follows:
CitymapsMapFragment fragment = (CitymapsMapFragment)fragmentManager.findFragmentById(R.id.map);
if (fragment != null) {
fragment.setMapViewListener(this);
}
I did not find the fragment have the method setMapViewListener but setMapViewReadyListener,does it right?
Other code:
CitymapsMapView mapView = new CitymapsMapView(this, options, this);
When I add animate in additional methods like this:
mapView.setMapPosition(position, 300, new MapViewAnimationListener() {
#Override
public void onAnimationEnd(boolean completed) {
Log.d("SomeApp", "Move Complete!");
}
});
the project fails and exits,I tried to surround the code with try-catch block to catch exception for purpose, but nothing shows in logcat view. Why?
I am developer on the Citymaps project. I will do my best to answer all your questions
1) If you are not receiving log statements, this is likely an issue with your own application, IDE, or device configuration. In our own application, which uses the Citymaps SDK, we have no issues with logging.
2) Prior to using the Citymaps SDK, it is highly advisable that you familiarize yourself with fragments, but the short version is that SupportCitymapsMapFragment extends from the Fragment class in the v4 support library.
3) It is up to you to set the default position the map.
4) If you create a class which implements from the LocationSource interface, and then call mapView.setLocationSource, you can modify the behaviors of the map's location services. For an example, have a look at CitymapsLocationSource.java, which is the default implementation for this interface used by the SDK.
As for the exception you are having, you have not provided nearly enough information. Please show a stack trace, and I may be able to help.
Thank you for using our SDK, feel free to post again with any more questions.
I'm using crosswalk now. I need to call a Java method when a user clicks a button in the HTML, which may look like:
Start
I'm not sure if Crosswalk extension is what I wanted, which seems to be so heavy-weighted just for calling a Java function.
Is there a simpler way to do this? Or should I use Cordova with Crosswalk in this case?
Here is about how to call java function with js in the crosswalk XWalkView. How to use XWalkView refer this answer.
References:
crosswalk-calling-java-methods-with-javascript
XWalkView manual
Below is the process to call java from js, and notices.
add this to activity you XWalkView in
webSettings.setJavaScriptEnabled(true);
mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
and this, you also make it a class
public class JsInterface {
public JsInterface() {
}
#JavascriptInterface
public void act() {
//do something
}
}
and in the html page
<button onclick="NativeInterface.act()">Call Java Here</button>
when you import JavascriptInterface, take care, make sure you imported the exact one.
import org.xwalk.core.JavascriptInterface;
Not this one, this is for webview
import android.webkit.JavascriptInterface;
If you import this one, this will cause no action when you operation on the page and below error in your android studio.
12-02 13:24:49.921 12376-12376/com.xxxxxx.app E/chromium:
[ERROR:xwalk_autofill_client.cc(121)] Not implemented reached in
virtual void xwalk::XWalkAutofillClient::OnFirstUserGestureObserved()
Usually when you import the JavascriptInterface, the first one is what we want just like below pic shows.
But sometime when you change from webview to XWalkView, you may forget to change the JavascriptInterface.
If you are only using XWalkView as an embedded view, the addJavascriptInterface is sufficient to inject Java object into XWalkView(JavaScript), which is just like the addJavascriptInterface in android.webkit.WebView:
https://crosswalk-project.org/apis/embeddingapidocs_v2/reference/org/xwalk/core/XWalkView.html
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference