I'm implementing two plugins in my unity project. First plugin is a live wallpaper which contain the main activity. The second plugin is a native sharing. What I did is I got the context reference from the first plugin which contain the main activity:
public static Context getContext()
{
instance.startActivity(myIntent);
return instance;
}
In unity script
Main_Context = appClass.CallStatic<AndroidJavaObject>("getContext");
and then I use the Main_Context reference to call a sharing function in the second plugin
new NativeShare().AddFile(Application.persistentDataPath + "/img Shot.png").SetSubject(" Image Share").SetText("").Share(Main_Context);
The problem is I keep getting this error:
Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
So it seems that I some how need to add this line in the second plugin just before the startactivity() call
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Is there an easier way to solve this error without editing an already compiled plugin ?
Alright, so it turns out the live wallpaper project and its Plugins extend the standard UnityPlayerActivity
class, which means the currentActivity
has been overridden and com.unity3d.player.UnityPlayer
no longer exist. So instead of getting the currentActivity
like this:
private static AndroidJavaObject Context
{
get
{
if( m_context == null )
{
using( AndroidJavaObject unityClass = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
{
m_context = unityClass.GetStatic<AndroidJavaObject>( "currentActivity" );
}
}
return m_context;
}
}
we can use the plug-ins that extended and override the standard UnityPlayerActivity
class:
private static AndroidJavaObject Context
{
get
{
if( m_context == null )
{
using( AndroidJavaObject unityClass = new AndroidJavaClass( "ulw.ulw.ulw.UnityPlayerActivity" ) )
{
m_context = unityClass.GetStatic<AndroidJavaObject>( "activity" );
}
}
return m_context;
}
Now all of the added plugins can coexist with the live wallpaper project and its Plugins.