Android簡單的分享筆記

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

http://blog.csdn.net/xyz_lmn/article/details/16856843採用Intent隱式調用Activity的方法,主要使用Intent.ACTION_SEND和Intent.createChooser();調用Android系統的分享介面。系統會過濾手機上的具有分享應用的程式,讓使用者進行選擇。如果沒有使用Intent.createChooser()則會取系統預設的使用者分享方式,只有未設定的情況下才會彈出讓使用者進行選擇。1、簡單的分享文本
1 Intent intent = new Intent();2 intent.setAction(Intent.ACTION_SEND);//設定Intent的ACTION3 intent.putExtra(Intent.EXTRA_TEXT, "YOUR SHARE TEXT");//分享的內容tag和內容4 intent.setType("text/plain");//標誌發送的資料類型,方便接收方進行處理(類型/子類型)5 startActivity(inten);//使用隱式調用Activity的方法
  1. 隱式調用Activity的方法
或者
  1. startActivity(Intent.createChooser(intent, "the chooser dialog title"));//
2、發送圖片則使用
  1. intent.putExtra(Intent.EXTRA_STREAM, imageUri);
  2. intent.setType("imag/*");//*可以為jpg,png等等
3、也可以同時分享一組資料
1 List<Uri> uris;2 Intent intent = new Intent();3 intent.setAction(Intent.ACTION_SED_MULTIPLE);//發送組4 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);5 intent.setType("*/*");//如果都是視頻則video/*6 startActivity(intent);
如果想讓應用進入彈出來的分享列表則需要在mainifest中activity添加intent-filter:例子:
 1 <intent-filter>   2                <action android:name="android.intent.action.SEND" />   3    4                <category android:name="android.intent.category.DEFAULT" />   5    6                <data android:mimeType="image/*" />   7            </intent-filter>   8          <intent-filter>   9                <action android:name="android.intent.action.SEND" />  10   11                <category android:name="android.intent.category.DEFAULT" />  12     13                <data android:mimeType="text/plain" />  14            </intent-filter>  15            <intent-filter>  16                <action android:name="android.intent.action.SEND_MULTIPLE" />  17   18                <category android:name="android.intent.category.DEFAULT" />  19   20                <data android:mimeType="image/*" />  21            </intent-filter>  
在接收的activity裡面接收
1 Intent intent = getIntent();2 String action = intent.getAction();//對應intent.setAction()3 String type = intent.getType();//對應intent.setType()4 inten.get???Extra(Intent.EXTRA_???)或者intent.getParcelableExtra(Intent.EXTRA_STREAM);

也可以把分享放到ActionBar裡面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.