Monodroid Study Notes (10)-use monodroid to call, send text messages, and send emails.

Source: Internet
Author: User

This time, we will discuss how to use the androidmanifest. xml file in monodroid. Here we will illustrate how to use phone calls, text messages, and emails as examples. The interface is as follows:

The layout file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <edittext Android: Id = "@ + ID/txtphoneno" <br/> Android: layout_width = "fill_parent" <br/> Android: phonenumber = "true" <br/> Android: hint = "Enter the phone number" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_margintop = "5px" <br/> Android: layout_marginleft = "5px"/> <br/> <edittext Android: id = "@ + ID/txtsms" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "100dip" <br/> Android: singleline = "false" <br/> Android: gravity = "TOP" <br/> Android: hint = "Enter text message content" <br/> Android: layout_below = "@ ID/txtphoneno"/> <br/> <button Android: Id = "@ + ID/btndial" <br/> Android: TEXT = "call" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_below = "@ ID/txtsms"/> <br/> <button Android: Id = "@ + ID/btnsendsms" <br/> Android: TEXT = "send SMS" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_below = "@ ID/txtsms" <br/> Android: layout_torightof = "@ ID/btndial"/> <br/> <button Android: id = "@ + ID/btnsendemail" <br/> Android: text = "send mail" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_below = "@ ID/txtsms" <br/> Android: layout_torightof = "@ ID/btnsendsms"/> <br/> <edittext Android: Id = "@ + ID/txtreceiver" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: hint = "Enter the recipient" <br/> Android: layout_below = "@ ID/btndial"/> <br/> <edittext Android: Id = "@ + ID/txttitle" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: hint = "Enter the email title" <br/> Android: layout_below = "@ ID/txtreceiver"/> <br/> <edittext Android: Id = "@ + ID/txtemail" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "100dip" <br/> Android: gravity = "TOP" <br/> Android: hint = "Enter the email body" <br/> Android: layout_below = "@ ID/txttitle"/> <br/> </relativelayout> 

"Phone call" is an essential function for every mobile phone. Although on the Android platform, you can use a program to make a variety of fascinating applications, but the most basic function is to call the phone, it is still a required course for every android engineer.

You must add uses-Permission to androidmanifest. xml and declare the permission to use Android: Name = "android. Permission. call_phone. The input of the underlying services on the mobile phone number is closely related to user privacy and call charges. Therefore, the program must obtain relevant permissions. Secondly, the "action_call" Key (Action) is introduced through the custom intent object and the URI. the parse () method is used to bring the user-entered phone number (data), and The startactivity () method is used to pass in the Custom intent ), you can call the phone through the program.

To check whether the phone format entered by the user is correct, isphonenovalid is added to activity1.

Protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> edittext txtphoneno = findviewbyid <edittext> (resource.id.txt phoneno); <br/> button btndial = findviewbyid <button> (resource. id. btndial); <br/> btndial. click + = (sender, e) =>< br/>{< br/> try <br/>{< br/> string input = txtphoneno. text; <br/> If (isphonenovalid (Indium Ut) <br/>{< br/> intent myintentdial = new intent ("android. intent. action. call ", android. net. uri. parse ("Tel:" + input); <br/> startactivity (myintentdial); <br/> txtphoneno. TEXT = ""; <br/>}< br/> else <br/> {<br/> toast. maketext (this, "The entered phone format is incorrect", toastlength. long ). show (); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/>}; <B R/>}< br/> bool isphonenovalid (string phoneno) <br/>{< br/> return RegEx. ismatch (phoneno, "^ //(? (// D {3 })//)? [-]? (// D {3}) [-]? (// D {5}) $ ") | RegEx. ismatch (phoneno," ^ //(? (// D {3 })//)? [-]? (// D {4}) [-]? (// D {4}) $ "); <br/>}< br/> 

The next step is the key. I have already told you that to make a call, you must add uses-Permission to androidmanifest. xml. What is androidmanifest. xml? If we use Java in eclipse to develop Android projects, androidmanifest. XML is indispensable. It contains the activity, service, or handler of the android application. You can understand it as ours. net winform app. config, web in Asp.net. config, which is a configuration file of the application. The plugin tool automatically generates androidmanifest. xml and generates corresponding XML elements for these attributes. For example, isn't there a [activity (Label = "monodroidtest", mainlauncher = true)] label in the upper part of our activity1 class? With this label, mandroid.exe will generate the following nodes in androidmanifest. xml when deploying a program:

<Activity Android: Name = ". activity1 "Android: Label =" monodroidtest "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" Android: intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> 

Therefore, some common XML elements, such as activity, application, and service, can be implemented by adding the corresponding attribute to your activity. However, for this uses-permission, monodroid does not provide the corresponding attribute. We can only manually add these elements by adding the androidmanifest. xml file.

Click "project" in vs2010 and select the last item, that is, the properties of your project. In the Project Properties window, click Android manifest. If you have not manually added androidmanifest. the XML file, as shown in:

It will prompt you that androidmanifest. XML is not found, and click to add. Click this link to display the following interface and add an androidmanifest. xml file under the properties directory.

Application name is your program name, package name is the installation package name, you can leave it blank, version number is the version number of the program, must fill in a number, version name is the name of the version, minimum Android version indicates the minimum Android version supported by the program. I chose 1.6 here. Install location does not know what to use, but this is the cause of a signature error when I deploy the program. In required permissions, selecting call_phone indicates the permission to call. Because we need to send text messages next, we can also select send_sms.

The parser will not automatically generate the file for us. That is to say, all attributes in the activity will become invalid. We need to complete the following:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" Android: versioncode = "1" Android: versionname = "1.0.0"> <br/> <application Android: label = "monodroidtest" Android: icon = "@ drawable/icon"> <br/> <activity Android: Name = ". activity1 "Android: Label =" monodroidtest "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" Android: intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "4"/> <br/> <uses-Permission Android: Name = "android. permission. call_phone "/> <br/> <uses-Permission Android: Name =" android. permission. send_sms "/> <br/> </manifest> 

Note that if you select 1.6 in minimum Android version just like you, then in androidmanifest. in the XML file, you must remove the Android: installlocation = "internalonly" attribute. I did not remove it, So errors occur during each deployment. After discussion with the technical staff on the official website, it should be a bug.

Okay, generate and deploy the program, enter the phone number in the first input box, and click the button. We can call the program ~~~

You can also use the "android. Action. dialer" method to call a virtual keyboard to make a call. You only need to change action. Call to Action. Dial when customizing intent.

In addition to making phone calls, another common function is to send text messages. The usage of text messages is similar to that of phone calls. The key is to use the Android. telephony. smsmanager class. If you have not added the text message permission to androidmanifest. XML, remember to add it here. The program code is as follows:

Button btnsendsms = findviewbyid <button> (resource. id. btnsendsms); <br/> btnsendsms. click + = (sender, e) =>< br/>{< br/> edittext txtsms = findviewbyid <edittext> (resource.id.txt SMS); <br/> If (! Isphonenovalid (txtphoneno. text) <br/>{< br/> toast. maketext (this, "The entered phone format is incorrect", toastlength. long ). show (); <br/> return; <br/>}< br/> else if (system. text. encoding. utf8.getbytecount (txtsms. text)> 140) <br/>{< br/> toast. maketext (this, "text message body out of range", toastlength. long ). show (); <br/> return; <br/>}< br/> try <br/>{< br/> android. telephony. smsmanager Sm = android. telephony. smsmanager. default; <br/> pendingintent Pi = pendingintent. getbroadcast (this, 0, new intent (), pendingintentflags. nocreate); <br/> SM. sendtextmessage (txtphoneno. text, null, txtsms. text, PI, null); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/> toast. maketext (this, "message sent successfully", toastlength. short ). show (); <br/>}; <br/> 

The pendingintent object is used here. It has the following features: When a pendingintent object is received, the broadcast action is performed, just like the context. the sendbroadcast () method is the same, which is why it is stored in smsmanager. in the sendtextmessage () method, the pendingintent must be input as one of the parameters of the shipping service.

Next we will customize the intent and use the Android. content. Intent. actionsend parameter to send emails through the mobile phone. In fact, the process of sending and receiving emails is through the built-in Gmail program of Android, rather than directly using the SMTP protocol. Because the current simulator does not have a built-in Gmail client program, it is normal that "No application can perform this action" will occur on the simulator after the email sending program sends data. I am running on my mobile phone and can send it normally.

Button btnsendemail = findviewbyid <button> (resource. id. btnsendemail); <br/> btnsendemail. click + = (sender, e) =>< br/>{< br/> edittext txtreceiver = findviewbyid <edittext> (resource.id.txt receiver); <br/> If (! RegEx. ismatch (txtreceiver. text, "^ // W + (-// W +) | (//. // W +) * // @ [A-Za-z0-9] + ((//. |-) [A-Za-z0-9] + )*//. [A-Za-z0-9] + $ ") <br/>{< br/> toast. maketext (this, "the input email address format is incorrect", toastlength. long ). show (); <br/> return; <br/>}< br/> edittext txttitle = findviewbyid <edittext> (resource.id.txt title ); <br/> edittext txtemail = findviewbyid <edittext> (resource.id.txt email); <br/> try <br/>{< br/> intent emailintent = new intent (Android. content. intent. actionsend); <br/> emailintent. settype ("plain/text"); <br/> emailintent. putextra (intent. extraemail, new string [] {txtreceiver. text}); <br/> emailintent. putextra (intent. extrasubject, txttitle. text); <br/> emailintent. putextra (intent. extratext, txtemail. text); <br/> startactivity (intent. createchooser (emailintent, "ojlovecd"); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/>}; <br/> 

In fact, text messages and emails are not limited to the above methods. You can try other methods according to the API documentation. I will not detail them here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.