How to implement the SHARE function of an application for Android?

Sometimes we see that after clicking the "share" button, a list of shared methods is displayed. And this list seems to be dynamically generated, not hardcoded.

For example, I have SpringPad installed on my phone and the application sharing feature allows me to share content using SpringPad, but how could he know that I have SpringPad?

How to implement such a function? Thanks.

+4
source share
2 answers

Here is the real code of my BBC News Android app that does this. It shares the URL on the page. If the user has the Facebook or Twitter applications installed, they will be given the opportunity to share these services, as well as with email, etc.

final Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, pageUrl); try { startActivity(Intent.createChooser(intent, "Select an action")); } catch (android.content.ActivityNotFoundException ex) { // (handle error) } 
+8
source

If you want to add your application to these lists, you need to declare <intent-filter> in the AndroidManifest.xml application and in your target activity in onCreate() you must handle this intention.

Example (I get it from an email application and edited a bit).

Documents of Intent and Intent Filters

About intent-filter as an AndroidManifest element

For an implementation, see the source of the Android email application (AndroidManifest.xml and src / com / android / email / activity / MessageCompose.java (in onCreate ()) files).

+4
source

Source: https://habr.com/ru/post/1347867/


All Articles