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 ...)
Thanks in advance.
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 ...)
Thanks in advance.
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()
Intent intent = getIntent();
String urlCompartido = intent.getStringExtra(Intent.EXTRA_TEXT);
or simply:
String urlCompartido = getIntent().getStringExtra(Intent.EXTRA_TEXT);
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);
}
}
}