通過意圖(Intent)啟動其他應用程序共享文本
infor from : http://androidbiancheng.blogspot.tw/2011/08/intent.html
有時我們希望與別人分享一些東西, 我們可以通過 Intent.ACTION_SEND 意圖(Intent), 設置類型"text/plain", 啟動其他共享應用程序, 例如 email, SMS...
實例:
實例:
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
| package com.AndroidShareText; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class AndroidShareTextActivity extends Activity { EditText TextInput; Button buttonShare; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); TextInput = (EditText)findViewById(R.id.input); buttonShare = (Button)findViewById(R.id.share); buttonShare.setOnClickListener( new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub String textToBeSent = TextInput.getText().toString(); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType( "text/plain" ); intent.putExtra(Intent.EXTRA_TEXT, textToBeSent); startActivity(Intent.createChooser(intent, "Share..." )); }}); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < EditText android:id = "@+id/input" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < Button android:id = "@+id/share" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Share" /> </ LinearLayout > |
沒有留言:
張貼留言