MonoDroid学习笔记(十)—— 使用MonoDroid打电话,发短信,发邮件。

这次我们来探讨一下MonoDroid中对AndroidManifest.xml文件的使用,这里以手机中较为常用的打电话,发短信及发邮件作为范例来进行说明。界面将如下所示:

MonoDroid学习笔记(十)—— 使用MonoDroid打电话,发短信,发邮件。_第1张图片

布局文件如下:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:layout_width="fill_parent"  
  4.                 android:layout_height="fill_parent">  
  5.   <EditText android:id="@+id/txtPhoneNo"  
  6.             android:layout_width="fill_parent"  
  7.             android:phoneNumber="true"  
  8.             android:hint="请输入电话号码"  
  9.             android:layout_height="wrap_content"  
  10.             android:layout_marginTop="5px"  
  11.             android:layout_marginLeft="5px"/>  
  12.   <EditText android:id="@+id/txtSMS"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="100dip"  
  15.             android:singleLine="false"  
  16.             android:gravity="top"  
  17.             android:hint="请输入短信内容"  
  18.             android:layout_below="@id/txtPhoneNo"/>  
  19.   <Button android:id="@+id/btnDial"  
  20.           android:text="拨打电话"  
  21.           android:layout_width="wrap_content"  
  22.           android:layout_height="wrap_content"  
  23.           android:layout_below="@id/txtSMS"/>  
  24.   <Button android:id="@+id/btnSendSMS"  
  25.           android:text="发送短信"  
  26.           android:layout_width="wrap_content"  
  27.           android:layout_height="wrap_content"  
  28.           android:layout_below="@id/txtSMS"  
  29.           android:layout_toRightOf="@id/btnDial"/>  
  30.   <Button android:id="@+id/btnSendEMail"  
  31.           android:text="发送邮件"  
  32.           android:layout_width="wrap_content"  
  33.           android:layout_height="wrap_content"  
  34.           android:layout_below="@id/txtSMS"  
  35.           android:layout_toRightOf="@id/btnSendSMS"/>  
  36.   <EditText android:id="@+id/txtReceiver"  
  37.             android:layout_width="fill_parent"  
  38.             android:layout_height="wrap_content"  
  39.             android:hint="请输入收件人"  
  40.             android:layout_below="@id/btnDial"/>  
  41.   <EditText android:id="@+id/txtTitle"  
  42.             android:layout_width="fill_parent"  
  43.             android:layout_height="wrap_content"  
  44.             android:hint="请输入邮件标题"  
  45.             android:layout_below="@id/txtReceiver"/>  
  46.   <EditText android:id="@+id/txtEMail"  
  47.             android:layout_width="fill_parent"  
  48.             android:layout_height="100dip"  
  49.             android:gravity="top"  
  50.             android:hint="请输入邮件正文"  
  51.             android:layout_below="@id/txtTitle"/>  
  52. </RelativeLayout>  
 

“打电话”是每个手机必备的功能,虽然在Android平台上可以通过程序,进行各种让人目眩神迷的应用,但拨打电话这项最基本的功能,依然是每个Android工程师的必修课程。

拨打电话的关键有两个方面:首先要在AndroidManifest.xml中添加uses-permission,并声明android:name="android.permission.CALL_PHONE"使用权限。由于拨打电话输入手机底层的服务,与用户隐私及通话费用等信息息息相关,因此程序必须取得相关权限。其次,通过自定义Intent对象,带入“ACTION_CALL"这个关键(ACTION),以及通过Uri.Parse()方法将用户输入的电话号码(Data)带入,最后以StartActivity()方法(将自定义的Intent传入),即可完成通过程序直接拨打电话的工作了。

为了检查用户输入是否正确的电话格式,Activity1中增加了一个方法IsPhoneNoValid。

[c-sharp]  view plain copy print ?
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     SetContentView(Resource.Layout.Main);  
  5.     EditText txtPhoneNo = FindViewById<EditText>(Resource.Id.txtPhoneNo);  
  6.     Button btnDial = FindViewById<Button>(Resource.Id.btnDial);  
  7.     btnDial.Click += (sender, e) =>  
  8.     {  
  9.         try  
  10.         {  
  11.             string input = txtPhoneNo.Text;  
  12.             if (IsPhoneNoValid(input))  
  13.             {  
  14.                 Intent myIntentDial = new Intent("android.intent.action.CALL", Android.Net.Uri.Parse("tel:" + input));  
  15.                 StartActivity(myIntentDial);  
  16.                 txtPhoneNo.Text = "";  
  17.             }  
  18.             else  
  19.             {  
  20.                 Toast.MakeText(this"输入的电话格式不正确", ToastLength.Long).Show();  
  21.             }  
  22.         }  
  23.         catch (System.Exception ex)  
  24.         {  
  25.             MessageBox.ShowErrorMessage(this, ex);  
  26.         }  
  27.     };  
  28. }  
  29. bool IsPhoneNoValid(string phoneNo)  
  30. {  
  31.     return Regex.IsMatch(phoneNo, "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$") || Regex.IsMatch(phoneNo, "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$");  
  32. }  
 

接下来就是关键了,刚才已经跟大家说过,要拨打电话,必须要在AndroidManifest.xml中添加uses-permission,那么这个AndroidManifest.xml是个什么东西?如果我们是在Eclipse里用Java来开发Android项目的话,AndroidManifest.xml是必不可少的,它里头包含这个Android应用程序具有哪些Activity,Service或者Receiver,你可以将它理解为我们.net里winform的app.config,asp.net里的web.config,它是应用程序的一个配置文件。MonoDroid为了简化我们手动修改AndroidManifest.xml文件的工作,将一些常用的xml元素封装成了.net里的Attribute类,在部署程序时,一个叫做mandroid.exe的工具会自动生成AndroidManifest.xml,并将这些Attribute生成相应的xml元素。例如,我们的Activity1类的上部不是有个[Activity(Label = "MonoDroidTest", MainLauncher = true)]标签吗?只要打上了这个标签,那么在部署程序时,mandroid.exe就会在AndroidManifest.xml中生成如下节点:

[xhtml]  view plain copy print ?
  1. <activity android:name=".Activity1" android:label="MonoDroidTest">  
  2.   <intent-filter>  
  3.     <action android:name="android.intent.action.MAIN" />  
  4.     <category android:name="android:intent.category.LAUNCHER" />  
  5.   </intent-filter>  
  6. </activity>  
 

因此,一些常用的xml元素,例如Activity,Application,Service等都可以通过在你的Activity中加上相应的Attribute来实现。但是像这次的uses-permission,MonoDroid并没有提供相应的Attribute,我们只能通过手动添加AndroidManifest.xml文件并自己加上这些元素了。

点击vs2010的菜单”项目“,选择最后一项,即你的项目的属性,在项目属性窗口中点击Android Manifest,如果你没有手动添加过AndroidManifest.xml文件,那么应该如下图所示:

MonoDroid学习笔记(十)—— 使用MonoDroid打电话,发短信,发邮件。_第2张图片

它会提示你没有找到AndroidManifest.xml,点击以新增。我们就点击一下这个链接,就会显示如下界面,并会在Properties目录下增加了一个AndroidManifest.xml文件。

MonoDroid学习笔记(十)—— 使用MonoDroid打电话,发短信,发邮件。_第3张图片

Application name是你的程序名称,Package name是安装包名称,可以不填,Version number是程序的版本号,必须填数字, Version name是版本的名称,Minimum Android version代表程序支持的最低Android版本,我这里选择的1.6,Install location不知道干吗用的,但就是这个东西造成我部署程序时产生了签名的错误。Required permissions里选择CALL_PHONE表示需要打电话的权限,由于我们接下来还要发短信,所以可以把SEND_SMS也勾选上。

我们来打开AndroidManifest.xml,里面已经为我们添加了一些东西,就是我们刚才在属性窗口里填的东西,但这样还是不完整的,因为只要我们手动添加了AndroidManifest.xml文件,mandroid.exe就不会为我们自动生成该文件,也就是说我们在Activity中加的一切Attribute将变为无效,需要我们来补充完整:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0.0">  
  3.   <application android:label="MonoDroidTest" android:icon="@drawable/icon">  
  4.     <activity android:name=".Activity1" android:label="MonoDroidTest">  
  5.       <intent-filter>  
  6.         <action android:name="android.intent.action.MAIN" />  
  7.         <category android:name="android:intent.category.LAUNCHER" />  
  8.       </intent-filter>  
  9.     </activity>  
  10.   </application>  
  11.   <uses-sdk android:minSdkVersion="4" />  
  12.   <uses-permission android:name="android.permission.CALL_PHONE" />  
  13.   <uses-permission android:name="android.permission.SEND_SMS" />  
  14. </manifest>  
 

注意如果你和我一样Minimum Android version里选的是1.6,那么在AndroidManifest.xml文件里一定要把android:installLocation="internalOnly"这个属性给去掉,我就是没去掉所以每次部署的时候才会出错,经与官网的技术人员讨论,应该是他们的一个bug。

好了,生成并部署程序,然后在第一个输入框里输入电话号码,点击,按钮,我们可以通过程序来进行打电话了~~~

MonoDroid学习笔记(十)—— 使用MonoDroid打电话,发短信,发邮件。_第4张图片

大家也可以使用”Android.Action.Dialer”的方式,调用虚拟键盘来拨打电话,只要在自定义Intent时将Action.CALL改为Action.DIAL即可。

除了打电话,另一个常用的功能就是发短信了。发短信的使用与打电话基本类似,关键是要使用Android.Telephony.SmsManager类来实现。如果刚才没有在AndroidManifest.xml中添加发短信的权限的话,这里记得添加上。程序代码如下:

[c-sharp]  view plain copy print ?
  1. Button btnSendSMS = FindViewById<Button>(Resource.Id.btnSendSMS);  
  2. btnSendSMS.Click += (sender, e) =>  
  3. {  
  4.     EditText txtSMS = FindViewById<EditText>(Resource.Id.txtSMS);  
  5.     if (!IsPhoneNoValid(txtPhoneNo.Text))  
  6.     {  
  7.         Toast.MakeText(this"输入的电话格式不正确", ToastLength.Long).Show();  
  8.         return;  
  9.     }  
  10.     else if (System.Text.Encoding.UTF8.GetByteCount(txtSMS.Text) > 140)  
  11.     {  
  12.         Toast.MakeText(this"短信正文超出范围", ToastLength.Long).Show();  
  13.         return;  
  14.     }  
  15.     try  
  16.     {  
  17.         Android.Telephony.SmsManager sm = Android.Telephony.SmsManager.Default;  
  18.         PendingIntent pi = PendingIntent.GetBroadcast(this, 0, new Intent(), PendingIntentFlags.NoCreate);  
  19.         sm.SendTextMessage(txtPhoneNo.Text, null, txtSMS.Text, pi, null);  
  20.     }  
  21.     catch (System.Exception ex)  
  22.     {  
  23.         MessageBox.ShowErrorMessage(this, ex);  
  24.     }  
  25.     Toast.MakeText(this"短信发送成功", ToastLength.Short).Show();  
  26. };  
 

这里使用到了PendingIntent对象,它具有下列特性:当接收到PendingIntent对象时,会进行Broadcast动作,就如同使用Context.SendBroadcast()方法一样,这样就是为什么在SmsManager.SendTextMessage()方法中需要传入PendingIntent作为传送服务的参数之一。

下面我们来通过自定义Intent,并使用Android.Content.Intent.ActionSend参数来实现通过手机发送电子邮件。实际上,收发EMail的过程是通过Android内置的Gmail程序,而非直接使用SMTP的协议。由于目前的模拟器并未内置Gmail Client端程序,也因此,发送EMail的程序在送出数据后,模拟器上会发生“No application can perform this action",这是正常的。我在我的手机上运行,是可以正常发送的。

[c-sharp]  view plain copy print ?
  1. Button btnSendEMail = FindViewById<Button>(Resource.Id.btnSendEMail);  
  2. btnSendEMail.Click += (sender, e) =>  
  3. {  
  4.     EditText txtReceiver = FindViewById<EditText>(Resource.Id.txtReceiver);  
  5.     if (!Regex.IsMatch(txtReceiver.Text, "^//w+((-//w+)|(//.//w+))*//@[A-Za-z0-9]+((//.|-)[A-Za-z0-9]+)*//.[A-Za-z0-9]+$"))  
  6.     {  
  7.         Toast.MakeText(this"输入的Email地址格式不正确", ToastLength.Long).Show();  
  8.         return;  
  9.     }  
  10.     EditText txtTitle = FindViewById<EditText>(Resource.Id.txtTitle);  
  11.     EditText txtEMail = FindViewById<EditText>(Resource.Id.txtEMail);  
  12.     try  
  13.     {  
  14.         Intent emailIntent = new Intent(Android.Content.Intent.ActionSend);  
  15.         emailIntent.SetType("plain/text");  
  16.         emailIntent.PutExtra(Intent.ExtraEmail, new string[] { txtReceiver.Text });  
  17.         emailIntent.PutExtra(Intent.ExtraSubject, txtTitle.Text);  
  18.         emailIntent.PutExtra(Intent.ExtraText, txtEMail.Text);  
  19.         StartActivity(Intent.CreateChooser(emailIntent, "ojlovecd"));  
  20.     }  
  21.     catch (System.Exception ex)  
  22.     {  
  23.         MessageBox.ShowErrorMessage(this, ex);  
  24.     }  
  25. };  
 

事实上,发短信和发邮件并不局限于以上方式,大家可以根据API文档试验一下其它的方式,我在这里就不详细说明了。

转载http://blog.csdn.net/ojlovecd/article/details/6329531

你可能感兴趣的:(学习笔记)