Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by jainam · Dec 17, 2013 at 07:41 AM · androidflashlighteclipsejartorch

problem accessing android jar in unity

hello developers!

I have an android project for camera flashlight, which when deployed from eclipse works fine. I am trying to access the flashlight function from my C# code in unity3d but it doesn't work. To verify if I am calling the android method correctly, I created a string function in the same activity and it is returning the string correctly. I am not familiar to native android coding. It would be great if you could have a look at the code and help me out.

I know there are some threads in unity forums and stackoverflow, I tried to find a solution in almost all the links I could find but no luck!

Below is android MainActivity.java (which I converted into a jar file from eclipse and copied in unity project, ~Assets/Plugins/Android/bin/),

 package com.example.newflash;
 
 import android.hardware.Camera;
 import android.hardware.Camera.Parameters;
 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Window;
 import android.view.WindowManager;
 
 public class MainActivity extends Activity {
 
     private static Camera camera;
     private static Parameters parameters;
     
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         //Hide the window title.
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     }
     
     public static String dummyString()
     {
         return "dummy string";
     }
     
     public static void setFlashOn()
     {
         if (camera == null)
             camera = Camera.open();
         parameters = camera.getParameters();
         parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
         camera.setParameters(parameters);
     }
     
     public static void setFlashOff()
     {
         parameters = camera.getParameters();
         parameters.setFlashMode(Parameters.FLASH_MODE_OFF);        
         camera.setParameters(parameters);
     }
 }



Below is my unity C# code,

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System;
 
 public class testJar : MonoBehaviour
 {
     bool torchon;
     AndroidJavaClass testClasslight;
     
     void Start ()
     {
         torchon = false;
     }
     
     void OnGUI ()
     {
         string teststring = "empty";
 
         AndroidJavaClass testClass = new AndroidJavaClass("com.example.glassplaces.MainActivity");
         teststring = testClass.CallStatic<string>("dummyString");
 
         GUI.Label (new Rect (20, 20, 100, 60), teststring);
         
         if(GUI.Button(new Rect (20, 100, 100, 60), "ON"))
         {
             torchon = true;
         }
         
         if(torchon == true)
         {
             GUI.Label(new Rect(200, 20,100,60), "torch ON");
             testClass.CallStatic<AndroidJavaObject>("setFlashOn");
         }
     }
 }



The permissions to access camera in AndroidManifest.xml when added, the app doesn't start at all. On excluding the xml file from the project, the "dummyString" method still returns the string.

Below is the AndroidManifest.xml,

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.newflash"
     android:versionCode="1"
     android:versionName="1.0" >
 
     <uses-sdk
         android:minSdkVersion="8"
         android:targetSdkVersion="18" />
 
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera" />
     <uses-feature android:name="android.hardware.camera.autofocus" />
 
     <application
         android:allowBackup="true"
         android:icon="@drawable/app_icon"
         android:label="@string/app_name">
         <activity
             android:name="com.example.newflash.MainActivity"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>
 
 </manifest>


Below is the warning which unity shows in console with the above xml included during Build & Run,

 Unable to find unity activity in manifest. You need to make sure orientation attribut is set to sensor manually.
 UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()


It would be great if someone could help me out. Any help is much appreciated.

Thank you in advance!

Jainam

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
Best Answer

Answer by stevethorne · Dec 17, 2013 at 01:45 PM

Your activity in Java has to be an activity that extends "UnityPlayerActivity", not your conventional "Activity". That is, com.unity3d.player.UnityPlayerActivity. You can do a search here for UnityPlayerActivity to find out more about this.

Comment
Add comment · Show 4 · 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 jainam · Dec 31, 2013 at 08:18 AM 0
Share

hi Steve! Thanks for the link! Following all the steps on the link, I still have the same results, string is called but flashlight doesn't work. I don't have experience in Android coding, so am confused about .so files. I have not generated .so files.. If *.so files are required how did the string function get called without them?

avatar image jainam · Jan 09, 2014 at 04:48 AM 0
Share

Hi Steve! Your link was really helpful. I found my mistake in the manifest file. now it's working. thanks!

avatar image toritobravom · Mar 20, 2014 at 01:42 PM 0
Share

How did you get it? I'm trying to reproduce your sample code, but when I press the torch button, doesn't occur anything. I extends from UnityPlayerActivity the Launcher Activity, it was what I changed from $$anonymous$$anifest. I'm working on Unity 4.3.4 and Nexus Tablet.

avatar image jainam · Mar 20, 2014 at 03:50 PM 0
Share

below is the working code

avatar image
0

Answer by jainam · Mar 20, 2014 at 03:50 PM

I asked this question on the forum as my code didn't work. So if you are using the above code, it won't work ;) Here is the working code...hope it helps :)

Create the package with the name "com.unity3d.player" in Android SDK.

Below is android MainActivity.java (which I converted into a jar file from eclipse and copied in unity project, ~Assets/Plugins/Android/bin/),

 package com.unity3d.player;
 
 import android.hardware.Camera;
 import android.hardware.Camera.Parameters;
 import android.os.Bundle;
 import android.view.WindowManager;
 import android.content.Context;
 import android.content.pm.PackageManager;
 
 public class MainActivity extends UnityPlayerActivity {
 
     private static Camera camera;
     private static Parameters parameters;
     static boolean s;
     
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         
         Context context = this;
         PackageManager packageManager = context.getPackageManager();
  
         // if device support camera?
         if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
             //yes
             s = true;
         }else{
             //no
             s = false;
         }
     }
     
     public static String dummyString()
     {
         return "dummy string";
     }
     
     public static String sendFlashStatus()
     {
         if(s==true)
         {
             return "has flash";
         }
         else
         {
             return "no flash";
         }
     }
     
     public static String setFlashOn()
     {
         if (camera == null)
             camera = Camera.open();
         parameters = camera.getParameters();
         parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
         camera.setParameters(parameters);
         
         return "";
     }
     
     public static String setFlashOff()
     {
         parameters = camera.getParameters();
         parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
         camera.setParameters(parameters);
         
         return "";
     }
 }


Below is my unity C# code,

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System;
 
 public class testJar : MonoBehaviour
 {
     AndroidJavaClass testClass;
     string toggleMethod, somestring, hasFlash;
     bool callOnce;
     
     void Start ()
     {
         testClass = new AndroidJavaClass("com.unity3d.player.MainActivity");
         hasFlash = testClass.CallStatic<String>("sendFlashStatus");
         
         toggleMethod = "setFlashOff";
     }
     
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.Escape))
             Application.Quit();
     }
     
     void OnGUI ()
     {
         somestring = testClass.CallStatic<string>("dummyString");
         GUI.Label (new Rect (20, 20, 100, 60), somestring);
         
         
         GUI.Label (new Rect (150, 20, 100, 60), ""+hasFlash);
         
         if(GUI.Button(new Rect (Screen.width/2-50, Screen.height-120, 100, 60), "Toggle torch"))
         {
             callOnce = true;
             if(toggleMethod == "setFlashOff")
             {
                 toggleMethod = "setFlashOn";
             }
             else
             {
                 toggleMethod = "setFlashOff";
             }
         }
 
         if(callOnce)
         {
             testClass.CallStatic<AndroidJavaObject>(""+toggleMethod);
             callOnce = false;
         }
     }
 }


Below is the AndroidManifest.xml,

 <?xml version="1.0" encoding="utf-8"?>
 <manifest
     xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.unity3d.player"
     android:installLocation="preferExternal"
     android:versionCode="1"
     android:versionName="1.0">
 
     <supports-screens
         android:smallScreens="true"
         android:normalScreens="true"
         android:largeScreens="true"
         android:xlargeScreens="true"
         android:anyDensity="true"/>
     
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     
     <application
         android:icon="@drawable/app_icon"
         android:label="@string/app_name"
         android:debuggable="true">
     <activity android:name="com.unity3d.player.MainActivity"
                   android:label="@string/app_name"
                   android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
     </activity>
     
   </application>
 </manifest>
Comment
Add comment · Show 2 · 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 toritobravom · Mar 20, 2014 at 04:58 PM 0
Share

Thanks!!! I've tested on two devices and it works on one. $$anonymous$$y next step, launch the camera android device.

avatar image nicolas-rios · Jul 14, 2016 at 05:24 PM 0
Share

@ jainam When I use the extends UnityPlayerActivity on Android Studio, It doesn't recognize it and it has an error. Is there something I need to reference in my project? Thanks

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Webcamtexture with Android flashlight 0 Answers

Webcam Texture and Flashlite (Android) 3 Answers

Android - Non main activity plugin not working: Class Not Found Exception 1 Answer

Calling static jar function from Unity3D 0 Answers

Unity3D:Field text or type signature not found when i use GetStatic to reach a string inside a Jar android plugin 0 Answers


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