转自:http://www.bdqn.cn/news/201311/12117.shtml
1
2
3
4
5
|
Intent
sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This
is my text to send." ); sendIntent.setType( "text/plain" ); startActivity(sendIntent); |
1
2
3
4
5
|
Intent
sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This
is my text to send." ); sendIntent.setType( "text/plain" ); startActivity(Intent.createChooser(sendIntent,
getResources().getText(R.string.send_to))); |
图一
图二
1
2
3
4
5
|
Intent
shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM,
uriToImage); shareIntent.setType( "image/jpeg" ); startActivity(Intent.createChooser(shareIntent,
getResources().getText(R.string.send_to))); |
1
2
3
4
5
6
7
8
9
|
ArrayList<Uri>
imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); //
Add your image URIs here imageUris.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.." )); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<activity android: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> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class ShareActivity extends Activity{ @Override protected void onCreate(Bundle
savedInstanceState) { //
TODO Auto-generated method stub super .onCreate(savedInstanceState); //
Get intent, action and MIME type Intent
intent = getIntent(); String
action = intent.getAction(); String
type = intent.getType(); if (Intent.ACTION_SEND.equals(action)
&& type != null )
{ if ( "text/plain" .equals(type))
{ handleSendText(intent); //
Handle text being sent } else if (type.startsWith( "image/" ))
{ handleSendImage(intent); //
Handle single image being sent } } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)
&& type != null )
{ if (type.startsWith( "image/" ))
{ handleSendMultipleImages(intent); //
Handle multiple images being sent } } else { //
Handle other intents, such as being started from the home screen } } void handleSendText(Intent
intent) { String
sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); String
sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE); if (sharedText
!= null )
{ //
Update UI to reflect text being shared } } void handleSendImage(Intent
intent) { Uri
imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); if (imageUri
!= null )
{ //
Update UI to reflect image being shared } } void handleSendMultipleImages(Intent
intent) { ArrayList<Uri>
imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (imageUris
!= null )
{ //
Update UI to reflect multiple images being shared } } } |