In order to promote the application, we often see the click-to-Share button will appear, such as Weibo, such as the sharing of the second list of applications, how this is achieved? This article will be introduced in detail.
Android's implementation sharing is done through an implicit activation activity.
Share text
1.action is action_send, the corresponding code:
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA"hello world"); sendIntent.setType("text/plain"); startActivity(sendIntent);
2. Change the list title of the share, implemented by Intent.createchooser (). The code is as follows:
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA"hello wrold"); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Benefits of using Intent.createchooser ()
1. You can change the title of the share list first.
2. All applications will be listed in the class, even if you have previously selected the default app.
Share pictures
1. The code is as follows:
Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file1)); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
2. Share a picture List
arraylist<uri> Imageuris = new Arraylist<uri> (); Imageuris. Add(IMAGEURI1);//Add your image URIs hereImageuris. Add(IMAGEURI2); Intent shareintent = new Intent (); Shareintent. Setaction(Intent. ACTION_send_multiple); Shareintent. Putparcelablearraylistextra(Intent. EXTRA_stream, Imageuris); Shareintent. SetType("image/*"); StartActivity (Intent. Createchooser(Shareintent,"Share images to ..."));
How to make our app appear in the list
By declaring Intent-filter
<activityandroid:name="com.example.sharedemo.ShareActivity"android: Label="@string/app_name" > <intent-filter> <action android:name="Android.intent.action.SEND" /> <category android:name="Android.intent.category.DEFAULT" /> <data android:mimetype="image/*" /> </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> <intent-filter> <action android:name="Android.intent.action.SEND_MULTIPLE" /> <category android:name="Android.intent.category.DEFAULT" /> <data android:mimetype="image/*" /> </intent-filter> </activity>
Android realizes the ability to share pictures and text