Search Unity

Creating a Share (button) Intent for Android in Unity that forces the Chooser

Discussion in 'Scripting' started by adamvisionstudios, Jun 24, 2015.

  1. adamvisionstudios

    adamvisionstudios

    Joined:
    Apr 24, 2014
    Posts:
    44
    Hey All,

    I have managed to utilize some code I found on this website to make a button in one of my games bring up the android share dialog.

    The Code is as follows:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class ShareApp : MonoBehaviour {
    5.  
    6. string subject = "eg. subject";
    7. string body = "eg. body text";
    8.  
    9. public void shareText()
    10.     {
    11.         //execute the below lines if being run on a Android device
    12.         #if UNITY_ANDROID
    13.         //Reference of AndroidJavaClass class for intent
    14.         AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent");
    15.         //Reference of AndroidJavaObject class for intent
    16.         AndroidJavaObject intentObject = new AndroidJavaObject ("android.content.Intent");
    17.         //call setAction method of the Intent object created
    18.         intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
    19.         //set the type of sharing that is happening
    20.         intentObject.Call<AndroidJavaObject>("setType", "text/plain");
    21.         //add data to be passed to the other activity i.e., the data to be sent
    22.         intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
    23.         intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body);
    24.         //get the current activity
    25.         AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
    26.         AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
    27.         //start the activity by sending the intent data
    28.         currentActivity.Call ("startActivity", intentObject);
    29.         #endif
    30.      
    31.     }
    32.  
    33. }
    My question is, when the code initiates in an app, it pulls up the android chooser, but also allows the user to set an app as default, which I don't want to do.

    Is there a way, with modification to this above code, I can force the share intent to ALWAYS bring up the chooser instead?

    I have found references to something called Intent.createChooser() but have absolutely no idea how to go about using it with the above code. I would greatly appreciate some help and even if possible some basic explanation as to the difference between the above code and the code on the Android developers pages outlining the sharing intent.

    Thanks

    Adam Nickerson
     
    Last edited: Jun 24, 2015
    DivyART, sjuyal, honor0102 and 2 others like this.
  2. adamvisionstudios

    adamvisionstudios

    Joined:
    Apr 24, 2014
    Posts:
    44
    Darn, I tried several other things with the code on the Android developer page, but I am stuck with bad context. Like my brain wont switch to understanding this code, I just stare at the two different styles and get confused as all heck. Any help would be greatly appreciated. Even if someone can just help me understand a little better so I can work towards figuring out a solution. Thanks
     
  3. adamvisionstudios

    adamvisionstudios

    Joined:
    Apr 24, 2014
    Posts:
    44
    I have tried using this code in various methods in my function

    Code (CSharp):
    1. Intent sendIntent = new Intent();
    2. sendIntent.setAction(Intent.ACTION_SEND);
    3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    4. sendIntent.setType("text/plain");
    5. startActivity(sendIntent);
    But I just can't seem to wrap my head around exactly how it works, or what it's doing, which is making it really difficult to understand why the other code works. It appears I have reached some kind of logic gap and could really use some help understanding.

    Thanks.
     
  4. adamvisionstudios

    adamvisionstudios

    Joined:
    Apr 24, 2014
    Posts:
    44
    Hi Everyone,

    I just wanted to give a heads up that I have solved this issue using code I found here.

    Instead of using this code at the last line:

    Code (CSharp):
    1.         currentActivity.Call ("startActivity", intentObject);
    2.  
    I now use this code:

    Code (CSharp):
    1.         AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "Share Via");
    2.         currentActivity.Call("startActivity", jChooser);
    It resolves the issue and now no longer asks for a default app to be set on the Chooser.
     
    DivyART, honor0102, IDmikael and 7 others like this.
  5. IvayloDev

    IvayloDev

    Joined:
    Apr 20, 2015
    Posts:
    23
    Thank you so much for the code. I didn't think it was that simple. Many Thanks ! :)
     
  6. Daniel.k

    Daniel.k

    Joined:
    Jun 17, 2015
    Posts:
    1
    Thank you so much. This is what I want.
     
  7. spark-man

    spark-man

    Joined:
    May 22, 2013
    Posts:
    96
    Thank you so much!
     
  8. erjoniske

    erjoniske

    Joined:
    Oct 2, 2016
    Posts:
    1
    Thank YOU!
     
  9. PaulWtp

    PaulWtp

    Joined:
    Jul 5, 2018
    Posts:
    1
    thank you so much
     
  10. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Thank you in 2019!
     
  11. pedroedte

    pedroedte

    Joined:
    Sep 12, 2019
    Posts:
    1
    Traduz pra mim!