How to get Native Activity of Unity in Android NDK?

Hello,

I would like to use the camera of Android to retrieve previews and apply some processing.
This action is only possible in Java and to retrieve preview, Camera instance needs to be attached to a SurfaceView. But to create a SurfaceView, we need the instance of the current Activity or at least, the context.

Unity will call a native function of the android library (like init_camera()). Next, this function will load and call the Java class responsible of the Camera management and preview retrieving. But I need to give to it the instance of the current Activity of Unity.

My question is: how to do that?

I will be very grateful if anyone could help me.
I know that Unity can access itself to android camera but it’s not what I want.

I found out that the java UnityPlayer.currentActivity returns the current Activity.
Does this method work in any java class in a plugin?

You need to create a class MainActivity inherited from UnityPlayerActivity.
Quick example :

package com.example.myplugin;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
                
public class MainActivity extends UnityPlayerActivity {
     
	@Override
	public void onCreate(Bundle savedInstanceState) {
    
		RelativeLayout m_Root = new RelativeLayout(this);
		m_Root.setId(1);

		this.addContentView(m_Root, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    
		SurfaceView mySurface = new SurfaceView(this);
		mySurface.setId(2);
		m_Root.addView(mySurface);

Then when you’ll build your plugin as a .jar you set your androidmanifest in your Assets/Plugins/Android/ folder in Unity.

For example :

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myplugin"
	android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

    <application
		android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
      
		
		<activity android:name="com.example.myplugin.MainActivity"
                  android:label="@string/app_name"
				  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		
    </application>
</manifest>