Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Android onActivityResult Callback for Unity

Discussion in 'Android' started by jbvobling, Jan 4, 2019.

  1. jbvobling

    jbvobling

    Joined:
    Sep 26, 2017
    Posts:
    27
    This is a sample code of sharing an image file and open the sharing UI.
    =============================================================================
    AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
    intentObject.Call<AndroidJavaObject>("setType", GetSetType(setType) );

    AndroidJavaClass fileProviderClass = new AndroidJavaClass("android.support.v4.content.FileProvider");
    AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");

    string packageName = unityContext.Call<string>("getPackageName");
    string authority = packageName + ".provider";

    string[] paths = new string[] { Application.persistentDataPath + "/CustomFiles/" + fileName };
    Debug.Log(paths[0]);

    AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", paths[0]);
    AndroidJavaObject uriObj = fileProviderClass.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);
    int FLAG_GRANT_READ_URI_PERMISSION = intentObject.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");
    intentObject.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObj);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObj);
    AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "");
    currentActivity.Call("startActivity", jChooser);
    =============================================================================
    This code works but my problem is that, I don't know how to call the onActivityResult callback. Anyone know what line of code must be used to make it work?
     
    asinghmodesttree likes this.
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    asinghmodesttree likes this.
  3. jbvobling

    jbvobling

    Joined:
    Sep 26, 2017
    Posts:
    27
    I tried this but nothing happens

    I changed currentActivity.Call("startActivity", jChooser);
    to
    currentActivity.Call("startActivityForResult", jChooser, 1);

    Then, I create a subclass:

    class UnityPlayerActivity : AndroidJavaProxy
    {
    public UnityPlayerActivity() : base("com.unity3d.player.UnityPlayer") { }

    public void onActivityResult(int requestCode, int resultCode, AndroidJavaClass resultIntent)
    {
    Debug.Log("test");
    }
    }

    it's not working. Any ideas?
     
    asinghmodesttree likes this.
  4. NinjaCat

    NinjaCat

    R&D Minion Unity Technologies

    Joined:
    Aug 22, 2013
    Posts:
    69
    com.unity3d.player.UnityPlayer is not an activity. You might want to take a look into the source of UnityPlayerActivity.java (it can be found in Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player of your unity installation).

    What you probably want is to extend the UnityPlayerActivity and not the UnityPlayer itself. You should also make sure that the extended activity is used instead of the default one.
     
    asinghmodesttree likes this.
  5. rahulrathaurkz

    rahulrathaurkz

    Joined:
    Dec 11, 2020
    Posts:
    2
    Can You Please Give Me A Example
     
  6. unity_F62923F8454F0E208D27

    unity_F62923F8454F0E208D27

    Joined:
    Sep 20, 2022
    Posts:
    2
    Sorry to raise an old thread but this seems unresolved. Did you ever get an example of sort this out? I am trying to do the same. I want to send a callback through unity to capture the activity results.
     
  7. sb33

    sb33

    Joined:
    Sep 9, 2021
    Posts:
    3
    I am also facing a similar issue, I created an instance of my plugin class from unity, which can access the UnityPlayerActivity context, I can call startActivity for result, but the onActivityResult never fires inside my code:



    Code (CSharp):
    1. /**
    2.      * Initiates the Google Sign-In process.
    3.      * This should be called from Unity to start the sign-in intent.
    4.      * @param GameObjectName the GameObject to receive the callback message
    5.      * @param UnityCallbackName the callback method to handle the login response, this method should accept GoogleSignInResponse
    6.      */
    7.     public void InitiateSignIn(String GameObjectName,String UnityCallbackName){
    8.         try{
    9.             Log.d("Native","Sign in called from unity");
    10.             // Start the sign-in intent using the context.
    11.             // For Unity plugin, you might need to call a Unity method to start the intent.
    12.  
    13.  
    14.             Intent signInIntent = client.getSignInIntent();
    15.             //signInIntent.setClass(context,AndroidGoogleSignIn.class);
    16.             //context.startActivity(signInIntent);
    17.             context.startActivityForResult(signInIntent,RC_SIGN_IN);
    18.  
    19.  
    20.             //Set Callback Params for result
    21.             this.GameObjectName = GameObjectName;
    22.             this.UnityCallbackName = UnityCallbackName;
    23.         }catch (ActivityNotFoundException e){
    24.             Log.d("Native", e.getLocalizedMessage());
    25.         }
    26.     }
    27.  
    28.  
    29. /**
    30. * Override so that the callback can catch the return data.
    31. * @param requestCode the code that tells the program which action is has return data
    32. * @param resultCode a result code of the operation
    33. * @param data the native intent
    34. */
    35. @Override
    36. protected void onActivityResult(int requestCode, int resultCode, Intent data){
    37.     try{
    38.         Toast.makeText(context, "On Activity Result Hit", Toast.LENGTH_SHORT).show();
    39.         Log.d("Native","Result Code: " + resultCode);
    40.         if (requestCode == RC_SIGN_IN) {
    41.             Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
    42.             handleSignInResult(task);
    43.         }
    44.     }catch(Exception e){
    45.         Toast.makeText(context, "onActivityResult error", Toast.LENGTH_SHORT).show();
    46.         Log.d("Native","onActivityResultError");
    47.         Log.d("Native",e.getLocalizedMessage());
    48.     }
    49.  
    50. }
    51.  
    52.  
    53.  
    54.  
    55.  
     
  8. ievgen_vakula

    ievgen_vakula

    Joined:
    Oct 31, 2022
    Posts:
    1
    Same problem.
    Also try to create activity class that extends 'UnityPlayerActivity' and override 'onActivityResult', but I couldn't import com.unity3d.player.UnityPlayerActivity.
    I just need to catch result of pin code intent :(:(:(
     

    Attached Files: