<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文:[http://developer.android.com/training/sharing/receive.html](http://developer.android.com/training/sharing/receive.html) 就像你的程序能夠發送數據到其他程序一樣,其也能夠方便的接收其他程序發來的數據。需要考慮的是用戶與你的程序如何進行交互,以及你想要從其他程序接收哪些數據類型。例如,一個社交網絡程序會希望能夠從其他程序接受文本數據,像一個有趣的網址鏈接。Google+的Android客戶端會接受文本數據與單張或者多張圖片。用這個app,用戶可以簡單的從Gallery程序選擇一張圖片來啟動Google+進行發布。 ### 更新你的manifest文件(Update Your Manifest) Intent filters通知Android系統一個程序愿意接受的數據。像上一課一樣,你可以創建intent filters來表明程序能夠接收哪些action。下面是個例子,對三個activit分別指定接受單張圖片,文本與多張圖片。(這里有不清楚Intent filter的,請參考[Intents and Intent Filters](http://developer.android.com/guide/topics/intents/intents-filters.html#ifs)) ~~~ <activity android:name=".ui.MyActivity" > <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> ~~~ 當另外一個程序嘗試通過創建一個intent并將其傳遞給startActivity來分享一些東西的時候,你的程序會被呈現在一個列表里面讓用戶進行選擇。如果用戶選擇了你的程序,相應的[activity](# "An activity represents a single screen with a user interface.")就應該被調用開啟,這個時候就是你如何處理獲取到的數據的問題了。 ### 處理接受到的數據(Handle the Incoming Content) 為了處理從Intent帶過來的數據,可以通過調用getIntent()方法來獲取到Intent對象。一旦你拿到這個對象,你可以對里面的數據進行判斷,從而決定下一步應該做什么。請記住,如果一個[activity](# "An activity represents a single screen with a user interface.")可以被其他的程序啟動,你需要在檢查intent的時候考慮這種情況(是被其他程序而調用啟動的)。 ~~~ void onCreate (Bundle 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); 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 } } ~~~ 請注意,因為你無法知道其他程序發送過來的數據內容是文本還是其他的數據,因此你需要避免在UI線程里面去處理那些獲取到的數據。更新UI可以像更新EditText一樣簡單,也可以是更加復雜一點的操作,例如過濾出感興趣的圖片。這完全取決于你的應用接下來要做些什么。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看