Receive URL in my App, when you click share in the Browser

4

When I browse with the Internet browser on my mobile phone, I have the possibility to share the url, by clicking on the button < some applications appear (Gmail, linkedin ...)

  • How can I make my application appear? When you press share
  • How can I receive the url in a field of my app?
  • Thanks in advance.

        
    asked by Felipe 28.09.2017 в 13:40
    source

    2 answers

    3
      

    How can I make my application appear when sharing?

    What you want is something similar to an Intent Chooser

    for this you have to add a Intent-Filter to the main Activity of your application for Receive an Intent of the type ACTION_SEND :

    <activity android:name="ShareActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
    </activity>
    

    You can see this in the documentation Reception of an implicit intent , for example by defining this in MainActivity :

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
    
        </activity>
    

    In this way, when you share, you can see your application as an option to do it:

      

    How can I receive the shared url, in a field of my app?

    To receive data that you previously sent using a Intent , these are received in the onCreate() method using the getIntent()

    method
     Intent intent = getIntent(); 
    String urlCompartido = intent.getStringExtra(Intent.EXTRA_TEXT);
    

    or simply:

     String urlCompartido  = getIntent().getStringExtra(Intent.EXTRA_TEXT);
    
        
    answered by 28.09.2017 / 20:28
    source
    3

    Try specifying an activity that will receive mimeType="text/plain" in the manifest:

    <activity android:name=".ActividadLink">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
    

    This means that the ActividadLink will be the activity that will be executed when a link is shared. Then to get the link it would be this way:

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            String link = intent.getStringExtra(Intent.EXTRA_TEXT);
                if (link!= null) {
    
                    textBox.setText(link);
                }
        }
    }
    
        
    answered by 28.09.2017 в 14:14