If you call Application.Quit() after a Google Play authentication, the app freeze for about 5 seconds. Too bad! That an ANR to the face. There's an open issue on github. Fortunately, there's a little workaround.
After some thought, I came to a workaround that seems to alleviate the problem, although it doesn't solve the underlying problem.
This is the workaround:
1) Make a new Android plugin. In this plugin you are going to send the Unity activity to the background.
public class ActivityUtils { private Activity activity; public ActivityUtils(Activity activity) { this.activity = activity; } public void finish() { activity.runOnUiThread(new Runnable() { @Override public void run() { activity.moveTaskToBack(true); } }); } }
2) From Unity Android, call the finish() instead of Application.Quit().
public class MyApplication { public static void QuitApplication() { #if UNITY_ANDROID AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject activityUtils = new AndroidJavaObject("com.google.unity.ActivityUtils", activity); activityUtils.Call ("finish"); #else Application.Quit(); #endif } }
How do we create a unity android plugin?
ReplyDeleteBasically you need to have:
Delete1) java jdk installed.
2) android sdk installed
But if you are already compiling to android from unity you have them installed, so no problem.
Now, to compile the plugin open a cmd terminal:
1) Compile the java code. Note that output_dir must exist before compiling. Also look what is your android.jar path.
> c:\path\to\java_jdk\bin\javac.exe path_to_your_java_files\files.java -bootclasspath C:\path\to\android_sdk\platforms\android-20\android.jar -d output_dir
2) Generate a .jar from the compiled files.
> c:\path\to\java_jdk\bin\jar.exe cvfM file_name.jar -c output_dir
After that, your .jar file is generated. Now you have to copy that .jar file to Assets\Plugins\Android in your project. You also need an AndroidManifest.xml file in that same directory.
There is an example of the content of the AndroidManifest.xml here:
http://docs.unity3d.com/Manual/PluginsForAndroid.html
Hope it helps. I think I'm going to make a more detailed tutorial on how to make android plugins. Stay tuned!
This comment has been removed by the author.
DeleteThank u so much!!!! It worked for me.
DeleteI have a suggestion about the code you have given...
#if UNITY_ANDROID
AndroidJavaClass playerClass =
new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity =
playerClass.GetStatic("currentActivity");
AndroidJavaObject activityUtils =
new AndroidJavaObject("com.google.unity.ActivityUtils", activity);
activityUtils.Call ("finish");
// Add Application.quit();
Application.quit();
Shouldn't we add Application.quit() function at the end?? or else the application will be running in the background after moving to the background thread.
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
DeleteAndroidJavaObject activity = jc.GetStatic("currentActivity");
activity.Call("moveTaskToBack" , true);
That will do the same thing, but less work
I wrote about a similar issue, I give you the link to my site. slots for real money
ReplyDeleteThank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. free games to win real money
ReplyDeleteBelow you will understand what is important, the idea provides one of the links with an exciting site:
ReplyDeletebetway casino
Best Unblocked Games For Schools
ReplyDelete