Android Plugin that provides a Webview

I’ve just started working with Plugins, and I just can’t get past this one issue. I can call functions that return strings just fine, I’ve been able to create a plugin to download files to the SD card (needed to use SSL), but I just can’t seem to create a plugin that provides a native view or intent in any way. This is my first foray into writing anything native for the droid, so hopefully I’m just missing something obvious.

I started with the Unity Plugin example using a prebuilt JNI Library that can be found here. I’m using the build file (and thus Apache ANT) to compile my java.

So, now I’ve got a C# file with the following code pertaining to this:

	...

	loadWebView		= AndroidJNI.GetMethodID(cls_JavaClass, "loadWebView", "(Ljava/lang/String;)Ljava/lang/String;");

	...

	private string openWebView() {

	JavaVM.AttachCurrentThread();

	// get the Java String object from the JavaClass object
	jvalue[] setTypeParameters =  new jvalue[2];
	setTypeParameters[0] = new jvalue();
	setTypeParameters[0].l = AndroidJNI.NewStringUTF("http://www.army.mil");
	//IntPtr str_fileInfo 	= JNI.CallObjectMethod(JavaClass, downloadFile, setTypeParameters);
	IntPtr str_fileInfo 	= AndroidJNI.CallObjectMethod(JavaClass, loadWebView, setTypeParameters);
	Debug.Log("str_fileInfo = " + str_fileInfo);

This calls the function - as I can remove all the java code and just have it return a string, and it works properly.

My java code, though, seems to just freeze up the application. The file it calls is as follows:

public class JavaClass {
private Activity mActivity;
public JavaClass(Activity currentActivity) {
	Log.i("JavaClass", "Constructor called with currentActivity = " + currentActivity);
	mActivity = currentActivity;
}

...

public String loadWebView(String fURL) {
	webActivity showWeb = new webActivity();
	showWeb.showWebView(fURL);
	return "loaded";
}

This then calls into another file which contains this code:

...
public class webActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void showWebView(String fURL) {    	
    	setContentView(R.layout.main);
        
        WebView wv = (WebView) findViewById(R.id.webview1);
        WebSettings webSettings = wv.getSettings();
        webSettings.setBuiltInZoomControls(true);
        wv.loadUrl(fURL);
    }
}

At some point, this Java all compiled. But I’ve gone through so many alterations trying to get things to work, I’ve ended up with things no longer compiling due to two errors on the line “webActivity showWeb = new webActivity();” (can’t find either symbol webActivity in that line). Even when it did compile, though, when I hit the button I had linked to call the Java code, it just froze.

In the end, I’m not sure what I’m missing here. I’m not completely shocked it’s not working, but I just can’t figure out for what reason (or reasons) it isn’t working due to my unfamiliarity with the native android code. Any help would be very, very much appreciated, as I’ve attempted about 5-6 completely different routes to this point, and this is the closest I’ve come.

Bear in mind that my knowledge of Unity and its JNI calls is about 1 hour old now. Not knowing anything more than what you’ve typed here, there are a few things I can think of.

 WebView wv = (WebView) findViewById(R.id.webview1);

Where did you define R.id.webview1? In a normal Android application, using Eclipse as the IDE, R is an automatically generated Resource bundle that is parsed out based on the contents of layout.xml files. I don’t see any such layout files in the linked sample project. I’m guessing that the build file created an Android project for you?

 webActivity showWeb = new webActivity();

New activities in Android are launched via Intents, not with the ‘new’ keyword. You’ll need to look at the documentation on how Intent objects are used to instantiate Activities. This leads into : What do you have in your AndroidManifest.xml file? Do you have one? You’ll need to create one and specifically let the main application know that it’s allowed to launch Intents. You also would need to let your webActivity have permissions to access the Internet.

I have no familiarity yet with using Apache ANT to build the project, so I’m afraid I don’t know exactly what you’re dealing with in terms of your project setup. If I have some time this evening I will check into the process and update my answer.

I think the following articles/tutorial from chinese, show that how to implement webview in Unity

Unity and WebView