- Home /
Launching an Android application from within unity without extending UnityPlayerActivity?
The documentation : Launching an Android application from within unity describes how to extend the UnityPlayerActivity to call the Intent method, but I want to do it without extending UnityPlayerActivity, by simply calling a Java class. but I can't succeed doing that.
C# code :
private static AndroidJavaClass ajc =new AndroidJavaClass ("com.MyCompany.AmazingApp.niceBuilder") ;
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass intent = ajc.CallStatic<AndroidJavaObject>("createIntent");
jo.Call(startActivity,intent);
Java Code :
public static Intent createIntent(){
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
return myIntent;
}
I also tried to start the activity completely from inside createIntent().
java code :
public static void createIntent(Activity currentActivity){
Intent myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
currentActivity.startActivity(enableBtIntent);
}
and then call the function from unity
ajc.CallStatic<void>("createIntent",jo);
but nothing works. I quote from the documentation :
"This intent then needs to be setup in the onCreate function"
could that be the reason, if so why, why do I need to setup my Intent in onCreate()?, and is there any solution to this problem?
see this nice plugin it may help you to launch any other app in android and ios... its without extending UnityPlayerActivity http://unitydevelopers.blogspot.in/2017/06/launch-any-app-from-unity-android-ios.html
Answer by techDev · Jun 11, 2014 at 07:07 PM
the following solution works fine for me :
In your Java class import UnityPlayer :
import com.unity3d.player.UnityPlayer;
and then you can get the current Activity and start a new intent :
Intent enableBtIntent = new Intent(YOURINTENT);
UnityPlayer.currentActivity.startActivity(enableBtIntent);
but if you want to check the result of your intent using:
UnityPlayer.currentActivity.startActivityForResult(intent, requestCode)
you then need to extend the UnityPlayerActivity to Override onActivityResult().
Your answer
Follow this Question
Related Questions
Android Multi-Plugins 0 Answers
How do I make an Android plugin for unity? 0 Answers