展示
源码
MainActivity.cs
var runExActivityBtn = FindViewById<Button>(Resource.Id.runExActivityBtn);
if (runExActivityBtn != null) runExActivityBtn.Click += (sender, args) => OnRunExActivity();
private void OnRunExActivity()
{
var intent = new Intent(this, typeof(ExActivity));
StartActivity(intent);
}
Scripts/ExActivity.cs
using System;
using System.Collections.Generic;
using System.IO;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Provider;
using Android.Widget;
using Java.Lang;
using Environment = Android.OS.Environment;
using File = Java.IO.File;
using Uri = Android.Net.Uri;
namespace android_by_csharp.Scripts
{
[Activity(Label = "ExActivity")]
public class ExActivity : Activity
{
private LinearLayout _linearLayout;
private const int MULTIMEDIA_MSG = 1; // 发送彩信标识
private const int SELECT_CONTACT = 2; // 联系人标识
private const int SEND_FILE = 3; // 发送附件标识
private const int TAKE_PHOTO = 4; // 拍照并保存标识
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.page);
_linearLayout = FindViewById<LinearLayout>(Resource.Id.linear_layout);
var btnInfos = new List<BtnInfo>
{
new BtnInfo("1.拨打电话", new Button(this), 1),
new BtnInfo("2.发送短信", new Button(this), 2),
new BtnInfo("3.发送彩信(带附件的短信,先选择相册图片)", new Button(this), 3),
new BtnInfo("4.打开浏览器", new Button(this), 4),
new BtnInfo("5.向单用户发送电子邮件", new Button(this), 5),
new BtnInfo("6.向多用户发送电子邮件", new Button(this), 6),
new BtnInfo("7.打开百度地图", new Button(this), 7),
new BtnInfo("8.进入无线网络设置界面", new Button(this), 8),
new BtnInfo("9.进入联系人页面", new Button(this), 9),
new BtnInfo("10.选择并查看指定联系人", new Button(this), 10),
new BtnInfo("11.打开Youtube软件", new Button(this), 11),
new BtnInfo("12.发送附件", new Button(this), 12),
new BtnInfo("13.拍照并保存(Android高版本)", new Button(this), 13),
new BtnInfo("14.拍照并保存(Android低版本)", new Button(this), 14),
};
foreach (var t in btnInfos)
{
_linearLayout?.AddView(t.Btn);
t.Btn.Click += (sender, args) => OnSwitchActivity(t.Type);
}
}
private void OnSwitchActivity(int type)
{
Uri uri;
Intent intent;
switch (type)
{
// 拨打电话
case 1:
uri = Uri.Parse("tel:10086");
intent = new Intent(Intent.ActionDial, uri);
StartActivity(intent);
break;
// 发送短信
case 2:
uri = Uri.Parse("smsto:10086");
intent = new Intent(Intent.ActionSendto, uri);
intent.PutExtra("sms_body", "Hello");
StartActivity(intent);
break;
// 发送彩信(带附件的短信,先选择相册图片)
case 3:
intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
intent.SetDataAndType(MediaStore.Images.Media.ExternalContentUri, "image/*");
StartActivityForResult(intent, MULTIMEDIA_MSG);
break;
// 打开浏览器
case 4:
uri = Uri.Parse("https://www.baidu.com");
intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
break;
// 向单用户发送电子邮件
case 5:
uri = Uri.Parse("mailto:admin@admin.com");
intent = new Intent(Intent.ActionSendto, uri);
intent.PutExtra(Intent.ExtraSubject, "Subject"); // 主题
intent.PutExtra(Intent.ExtraText, "Hello"); // 内容
StartActivity(intent);
break;
// 向多用户发送电子邮件
case 6:
intent = new Intent(Intent.ActionSend);
intent.PutExtra(Intent.ExtraEmail, new[] { "000@admin.com", "111@admin.com" }); // 收件人
intent.PutExtra(Intent.ExtraCc, new[] { "222@admin.com", "333@admin.com" }); // 抄送
intent.PutExtra(Intent.ExtraBcc, new[] { "444@admin.com", "555@admin.com" }); // 密送
intent.PutExtra(Intent.ExtraSubject, "Subject"); // 主题
intent.PutExtra(Intent.ExtraText, "Hello"); // 内容
intent.SetType("message/rfc822"); // 邮件类型
StartActivity(intent);
break;
// 打开百度地图
case 7:
uri = Uri.Parse(
"baidumap://map/direction?destination=latlng:39.98871,116.43234|name:北京&mode=driving");
intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
break;
// 进入无线网络设置界面
case 8:
intent = new Intent(Settings.ActionWirelessSettings);
StartActivity(intent);
break;
// 进入联系人页面
case 9:
intent = new Intent();
intent.SetAction(Intent.ActionView);
intent.SetData(ContactsContract.Contacts.ContentUri);
StartActivity(intent);
break;
// 选择并查看指定联系人
case 10:
intent = new Intent(Intent.ActionPick, ContactsContract.Contacts.ContentUri);
StartActivityForResult(intent, SELECT_CONTACT);
break;
// 打开Youtube软件
case 11:
uri = Uri.Parse("vnd.youtube:");
intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
break;
// 发送附件
case 12:
intent = new Intent(Intent.ActionGetContent);
intent.SetType("*/*"); // 无类型限制
intent.AddCategory(Intent.CategoryOpenable);
StartActivityForResult(intent, SEND_FILE);
break;
// 拍照并保存(Android高版本)
case 13:
intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, TAKE_PHOTO);
break;
// 拍照并保存(Android低版本)
case 14:
intent = new Intent(MediaStore.ActionImageCapture);
var file1 = new File($"{Environment.ExternalStorageDirectory?.AbsolutePath}/Download/",
$"{JavaSystem.CurrentTimeMillis()}.jpg");
uri = Uri.FromFile(file1);
intent.PutExtra(MediaStore.ExtraOutput, uri);
StartActivity(intent);
break;
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Uri uri;
Intent intent;
File file;
switch (requestCode)
{
// 3.发送彩信
case MULTIMEDIA_MSG when resultCode == Result.Ok:
intent = new Intent(Intent.ActionSend);
intent.PutExtra("sms_body", "Hello");
uri = Uri.Parse(data.DataString);
intent.PutExtra(Intent.ExtraStream, uri);
intent.SetType("image/*");
StartActivity(intent);
break;
// 10.选择并查看指定联系人
case SELECT_CONTACT when resultCode == Result.Ok:
var contactId = Convert.ToInt64(data?.Data?.LastPathSegment);
uri = ContentUris.WithAppendedId(ContactsContract.Contacts.ContentUri, contactId); // 二参为联系人ID
intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
break;
// 12.发送附件
case SEND_FILE when resultCode == Result.Ok:
uri = Uri.Parse(data.DataString);
intent = new Intent(Intent.ActionSend);
intent.PutExtra(Intent.ExtraSubject, "Subject"); // 主题
intent.PutExtra(Intent.ExtraText, "Hello"); // 内容
intent.PutExtra(Intent.ExtraStream, uri); // 附件
intent.SetType("image/*");
StartActivity(intent);
break;
// 13.拍照并保存(Android高版本)
case TAKE_PHOTO when resultCode == Result.Ok:
var bitmap = (Bitmap)data.Extras?.Get("data");
var imageView = new ImageView(this);
imageView.SetImageBitmap(bitmap);
_linearLayout.AddView(imageView);
// 保存图片
byte[] bitmapData;
var memoryStream = new MemoryStream();
bitmap?.Compress(Bitmap.CompressFormat.Png, 0, memoryStream);
bitmapData = memoryStream.ToArray(); // bitmap转byte数组
file = new File(
$"{GetExternalFilesDir(Environment.DirectoryPictures)}/{JavaSystem.CurrentTimeMillis()}.png");
Toast.MakeText(this, file.Path, ToastLength.Short)?.Show();
var fileStream = new FileStream(file.Path, FileMode.CreateNew);
fileStream.Write(bitmapData, 0, bitmapData.Length);
break;
}
}
}
public class BtnInfo
{
public readonly Button Btn;
public readonly int Type;
public BtnInfo(string label, Button btn, int type)
{
Btn = btn;
Btn.Text = label;
Btn.TextSize = 12;
Type = type;
}
}
}