[Xamarin] 「分享」功能

  • 276
  • 0
  • 2016-07-06

 APP可以「分享」文字或圖片給其他的APP,如下圖,

1. 分享的功能,當麻許的超技八 - [Xamarin] 簡單的分享方法,裡面有詳細的介紹,網址如下:
    http://no2don.blogspot.com/2015/07/xamarin.html

2. 如果您寫的app,是接受其他APP分享來的資料,需要在Activity類別 加上下面這行:

[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataMimeType = "*/*")]

請點擊下圖,即可瞭解其用法:

DataMimeType  是指定傳過來的資料是哪種資料類型,例如 文字可以用 text/plain,圖片就可以用 image/*

3. 處理分享過來的文字或圖片,程式碼如下:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    //讀取其他APP分享來的文字
    if (!String.IsNullOrEmpty(Intent.GetStringExtra(Intent.ExtraText)))
    {
        string subject = Intent.GetStringExtra(Intent.ExtraText);
        Toast.MakeText(this, subject, ToastLength.Long).Show();
    }

    //讀取其他APP分享來的Image
    if (Intent.Action == Intent.ActionSend && Intent.Extras.ContainsKey(Intent.ExtraStream))
    {          
        Android.Net.Uri uri = (Android.Net.Uri)Intent.Extras.GetParcelable(Intent.ExtraStream);
        ImageView im = FindViewById<ImageView>(Resource.Id.imageView1);              
        im.SetImageURI(uri);