Android使用系統Intent實現分享功能及將應用加入分享列表++分享郵箱實現

來源:互聯網
上載者:User

如何給應用增加分享功能,怎樣將應用加入系統的分享挑選清單?

Intent.createChooser()方法用來彈出系統分享列表。

查看Intent對應的組件是否存在,可查看Android判斷Intent是否存在,是否可用

1、應用增加分享功能

 
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FenxiangActivity extends Activity {    private Button btnFenXiang = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btnFenXiang = (Button) findViewById(R.id.btnFenXiang);        btnFenXiang.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Intent.ACTION_SEND); // 啟動分享發送的屬性                intent.setType("text/plain"); // 分享發送的資料類型                intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); // 分享的主題                intent.putExtra(Intent.EXTRA_TEXT, "extratext"); // 分享的內容                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 這個也許是分享列表的背景吧                FenxiangActivity.this.startActivity(Intent.createChooser(                        intent, "分享"));// 目標應用選擇對話方塊的標題            }        });    }

 

 

 

上面的代碼為分享文本,若想分享圖片資訊需要設定setType為“image/*”,傳遞一個類型為Uri的參數Intent.EXTRA_STREAM。

2、應用加入系統分享列表
只需在AndroidManifest.xml中加入以下代碼:

1
2
3
4
5
6
7
<activity android:name=".SharePage" android:label="分享到微博">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

  下載 地址 :http://files.cnblogs.com/firecode/Fenxiang.rar

 

 

 

Android:“分享到郵箱”的實現

 

這是一個簡單的分享到郵箱的實現,你可以附上自己的圖片。

  1. //cacheDir是你所要共用的檔案對象所在的目錄
  2. //你可以用自己的檔案對象覆蓋File f
  3. File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), getString(getApplicationInfo().labelRes)); 
  4. File f = new File(cacheDir, "image_name.jpg"); 
  5.  
  6. Intent intent = new Intent(Intent.ACTION_SEND);
  7. intent.setType("image/jpeg");
  8. intent.putExtra(Intent.EXTRA_TEXT, "Email body over here");
  9. intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject over here");
  10. intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
  11. startActivity(Intent.createChooser(intent, "Share via:"));

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.