作者 : 林致宇 日期 :2012/1/23 問題 : 如何分享分享文字資訊給其它的應用程式? 解答 : 有時候我們可能會希望 我們的應用程式的資訊 能夠讓使用者分享出去, 讓使用者能夠將此資訊傳送簡訊給其好友或寄電子郵件的方式寄給其好友, 這份文件將示範如何寫出這樣的應用程式 本文件將產出兩個 App, 第一個 App 是 分享資料的來源, 名為 CopyPaste_Source, 功能非常簡單, 主畫面只有一段文字及一個按鈕, 如下圖 ( 左 ) 所示, 當按下按鈕後, 會出現一個對話框, 如下圖 ( 右 ) 所示, 這個對話框有一個文字編輯元件 (EditText) 與一個按鈕, 文字編輯元件的預設內容是主畫面那段文字的內容, 使用者被允許在分享前做一些編輯, 當編輯好欲分享的文字內容後, 就可按下 分享文字給 按鈕 按下 分享文字給 按鈕後, 會出現一個應用程式列表, 如下圖所示, 這些應用 程式都可接收 CopyPaste_Source 所分享的文字
筆者嘗試點選幾個應用程式, 讓讀者更能感受那些應用程式是如何接收分享的文字, 下面依序是點選 訊息 ( 簡訊 ) Gmail LINE 以及 Evernote 的結果 點選 訊息 ( 簡訊 ) 後, 分享的文字會成為簡訊的內容 ; 點選 Gmail 後, 分享的文字會成為信件的內容 ; 點選 LINE 後, 分享的文字會成為聊天的內容 ; 點選 Evernote 後, 分享的文字會成為記事的內容
接下來我們就來看如何寫出 CopyPaste_Source 這樣的應用程式, 程式碼如下 所示 : 1 package lincyu.copypaste.source;
2 3 import android.app.activity; 4 import android.app.alertdialog; 5 import android.content.intent; 6 import android.os.bundle; 7 import android.view.layoutinflater; 8 import android.view.view; 9 import android.view.view.onclicklistener; 10 import android.widget.button; 11 import android.widget.edittext; 12 import android.widget.textview; 13 14 public class CopyPaste_Source extends Activity { 15 16 TextView tv_msg; 17 EditText et_sharetext; 18 19 public void oncreate(bundle savedinstancestate) { 20 super.oncreate(savedinstancestate); 21 setcontentview(r.layout.main); 22 23 tv_msg = (TextView)findViewById(R.id.tv_msg); 24 Button btn_opendialog = 25 (Button)findViewById(R.id.btn_open_dialog); 26 btn_opendialog.setonclicklistener(new OnClickListener() { 27 public void onclick(view v) { 28 opendialog(); 29 } 30 }); 31 } 32 33 void opendialog() { 34 LayoutInflater li = LayoutInflater.from(this); 35 View dialogview= li.inflate(r.layout.dialog, null); 36 37 et_sharetext = 38 (EditText)dialogView.findViewById(R.id.et_sharetext); 39 et_sharetext.settext(tv_msg.gettext());
40 Button btn_shareto = 41 (Button)dialogView.findViewById(R.id.btn_share); 42 btn_shareto.setonclicklistener(new OnClickListener() { 43 public void onclick(view v) { 44 String sharetext = 45 et_sharetext.geteditabletext().tostring(); 46 Intent intent = new Intent(Intent.ACTION_SEND); 47 intent.settype("text/plain"); 48 intent.putextra(intent.extra_text, sharetext); 49 startactivity( 50 Intent.createChooser(intent, " 分享 ")); 51 } 52 }); 53 54 AlertDialog.Builder ad = 55 new AlertDialog.Builder(this); 56 ad.setview(dialogview); 57 ad.show(); 58 } 59 } 比較需要解說的, 應該只有 分享文字給 這個按鈕的傾聽者, 程式碼位於 43~51 行, 第 44 行,shareText 變數所儲存的就是欲分享的文字, 接著就是叫出 應用程式列表 (Chooser), 首先我們要宣告一個 Implicit Intent, 這個 Intent 的 ACTION 為 Intent.ACTION_SEND, 如第 46 行所示, 接著在第 47 行, 我們設定分享資料的 MIME type, 由於我們所傳送的資料是純文字, 設成 text/plain 即可, 關於其它資料的 MIME type 可參考 [1], 第 48 行, 利用 Intent 類別的 putextra 方法把分享的文字帶進去, 即完成所有 Intent 的設定, 這個 Intent 是要送給處理分享文字的應用程式 ( 例如 訊息 ( 簡訊 ) Gmail LINE 等), 而不是不是叫出 應用程式列表 用的, 要叫出 應用程式列表 (Chooser), 必須宣告另一個 Implicit Intent,Intent 的 ACTION 為 Intent.ACTION_CHOOSER,Intent 類別提供了 Static 方法叫 createchooser,createchooser 的第一個參數是真正要丟給選擇的應用程式 ( 例如 訊息 ( 簡訊 ) Gmail LINE 等) 的 Intent, 我們只要把第 46 行所產生的 Intent 丟進去就可以了,createChooser 的第二個參數是 應用程式列表 (Chooser) 的標題, 如下圖紅色圈起來的地方所顯示的文字 :
以上就是 CopyPaste_Source 需要講解的部份, 其它部份的程式碼, 讀者可自行閱讀 然而讀者可能馬上想到一個問題 : 我們有沒有可能寫一個 App( 像 訊息 ( 簡訊 ) Gmail LINE 那樣 ), 能夠讀取其它應用程式所分享的文字?, 本文件的第二個範例 CopyPaste_Destination 就是介紹如何寫出這樣的 App 有兩個重點 : AndroidManifest.xml 的撰寫方法 主程式的撰寫方法 首先我們來看 AndroidManifest.xml, 檔案內容如下 : 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 package="lincyu.copypaste.destination" 5 android:versioncode="1" 6 android:versionname="1.0" > 7 <uses-sdk android:minsdkversion="7" /> 8 9 <application 10 android:icon="@drawable/ic_launcher" 11 android:label="@string/app_name" >
12 <activity 13 android:name=".copypaste_destination" 14 android:label="@string/app_name" > 15 <intent-filter> 16 <action android:name="android.intent.action.main" /> 17 <category android:name="android.intent.category.launcher" /> 18 </intent-filter> 19 </activity> 20 <activity 21 android:name=".copypaste_destination" 22 android:label="@string/app_name" > 23 <intent-filter> 24 <action android:name="android.intent.action.send" /> 25 <category android:name="android.intent.category.default" /> 26 <data android:mimetype="text/plain" /> 27 </intent-filter> 28 </activity> 29 </application> 30 </manifest> 我們將重點放在 20~28 行處, 透過 CopyPaste_Source, 我們知道, 能夠處理分享文字的 App 會接收到一個 ACTION 為 Intent.ACTION_SEND 的 Implicit Intent, 因此 23~27 行的 <intent-filter> 內, 我們必須設定這個 Activity 能夠處理 ACTION 為 Intent.ACTION_SEND( 即 android.intent.action.send) 的 Intent, 第 26 行我們更進一步指定能夠處理的資料為 text/plain 接下來我們來看主程式的寫法, 程式碼如下所示 : 1 package lincyu.copypaste.destination; 2 3 import android.app.activity; 4 import android.content.intent; 5 import android.os.bundle; 6 import android.widget.textview; 7 8 public class CopyPaste_Destination extends Activity { 9 /** Called when the activity is first created. */ 10 @Override 11 public void oncreate(bundle savedinstancestate) {
12 super.oncreate(savedinstancestate); 13 setcontentview(r.layout.main); 14 15 TextView tv_msg = (TextView)findViewById(R.id.tv_receive); 16 Intent intent = getintent(); 17 if (intent.getaction().equals(intent.action_send)) { 18 tv_msg.settext( 19 intent.getstringextra(intent.extra_text)); 20 } 21 } 22 } 需要解說的應該只有 16~20 行的地方, 首先在第 16 行, 呼叫 Activity 類別的 getintent 方法來取得傳入的 Intent, 如果這個 Intent 的 ACTION 是 Intent.ACTION_SEND ( 第 17 行 ), 我們在第 19 行呼叫 Intent 類別的 getstringextra 方法將分享的文字取出並印出來,getStringExtra 的參數帶入 Intent.EXTRA_TEXT 即可 下面是執行結果, 首先我們先執行 CopyPaste_Source, 按下 分享文字給 按鈕後, 我們寫的 CopyPaste_Destinaion 出現在應用程式列表了, 如下圖 ( 左 ) 所示, 點選後, CopyPaste_Destinaion 就會被啟動, 分享的文字被印出來,, 如下圖 ( 右 ) 所示
參考資料 [1] Webmaster Toolkit :: listing of mime types, http://www.webmaster-toolkit.com/mime-types.shtml