问题
I know that Codename One and Android are completely different frameworks. But the question is, How can I access in Codename One classes from native Android?
eg: I want to create a method called onCreat()
that looks like this:
public void onCreate(Bundle savedInstanceState){}
In Android you have to import the package import android.os.Bundle;
How can I integrate this package (for onCreat()
) and other packages from Android in codenamed One?
What should I set in Code Name One that I can access these classes. Is this possible?
Edit:
An other Example:
import android.net.Uri;
public void startWeb(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
How can i convert this Android code in Codename One so that it looks like this and, if i click on the Button, works:
protected void onMain_Button6Action(Component c, ActionEvent event) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
The point is that I want to understand how I make the Android code for Codename One functional.
Thanks.
回答1:
You need to use native interfaces to access the android native functionality. However, the onCreate call is a special case since its a callback from the lifecycle of the Android application and will be invoked to launch the Codename One start/init methods and will not invoke your code.
To listen to those lifecycle calls you can use the AndroidNativeUtil class which has a lifecycle listener that allows you to access those features from the native interfaces.
回答2:
package com.mycompany.ecopay;
import android.content.Intent;
import android.net.Uri;
import com.codename1.impl.android.AndroidNativeUtil;
import com.codename1.impl.android.CodenameOneActivity;
public class NativeAccessImpl {
public void pay() {
com.codename1.impl.android.AndroidNativeUtil.getActivity().runOnUiThread(new
Runnable() {
public void run() {
// your code goes here
CodenameOneActivity aActivity = (CodenameOneActivity)
AndroidNativeUtil.getActivity();
android.content.Intent intent = new
android.content.Intent(Intent.ACTION_CALL, Uri.parse("tel:0779083353"));
aActivity.startActivity(intent);
}
});
}
public boolean isSupported() {
return true;
}
来源:https://stackoverflow.com/questions/27600380/access-and-use-in-codename-one-native-android-classes