Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by barryllium · Jun 08, 2011 at 04:37 AM · androidpluginwebview

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by maskedMan · Nov 23, 2011 at 09:48 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by geminids · Nov 15, 2012 at 08:47 PM

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

Unity and WebView

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Android webview on Unity 0 Answers

Android: NullRefException on creating WebView 0 Answers

Plugin Android that doesn't work anymore in Unity 4.3 0 Answers

How to add i-ad for android unity game? 0 Answers

Android native plugin question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges