Android Developers believe that Java is the first choice,. for Net developers, to use C # for development on the Andriod platform, Mono provides the corresponding development platform for implementation. Mono for Andriod is dedicated.. Net developer-designed Android mobile development platform.
Mono for Andriod official documentation: http://docs.xamarin.com/android
Mono for Andriod is fully bound to the Andriod Java API. Therefore, when you install Mono for Andriod, you must first install JDK and Andriod SDK so that development can proceed smoothly.
Mono for Andriod can interact with Java, C/C ++, that is, Mono for Andriod can directly call Java, C, C ++ class libraries.
Mono for Android applications are based on. Net BCL, which is the Base Class Library. It supports XML, Database, Serialization, IO, String, and Networking. Its integrated development environment is MonoDevelop or Visual Studio.
Mobile cross-platform support, such as Mono for Andriod and MonoTouch (C # IOS development platform) can share a set of background public code. Of course, I believe it will also be used for Windows Phone 7 in the future.
Mono for Andriod is built on Mono. Mono is an open-source version of. NET Framework Based on. Net ECMA. It can run on Linux, UNIX, FreeBSD, Mac OSX, and so on.
When you compile a Mono for Andriod application, there are three main steps:
1. The source File is a pre-compiled part. aresgen.exe runs the Resource Directory, such as Resource. Designer. cs, UI File, Images, and other Resource files.
2. compile it into a managed. Net or Mono application assembly
3. Create the Andriod installation package: generate an APK file and package it with the mandroid.exe tool.
Mono for Andriod Installation
Currently, installation in Windows and Mac is supported. Here, Windows is used as an example:
: Http://xamarin.com/trial
You can also refer to its installation documentation: http://docs.xamarin.com/android/getting_started/installation/Installation_for_Windows
In this case, I select the Enterprise Edition Installation File setup.exe.
First, it will monitor whether your computer has been installed with relevant preparations, including JDK (it is said that Java 7 is not supported currently, but you have not tried it because the computer has already installed JDK 6), Andriod SDK, MonoDevelop 3.0.2, Mono Andriod Tool, etc. We recommend that you install all the versions in the Andriod SDK to debug applications of different versions.
After the installation is complete, you can see in the assembly path C: \ Program Files \ Reference Assemblies \ Microsoft \ Framework \ MonoAndroid of MonoAndriod Framework:
In addition, mono. android. jar can be found in the directory, indicating that mono also supports calling Jar packages.
The mono. Andriod packages of all android versions have been installed locally.
Now, you can open an AVD Manager window in Monodevelop or VS. In this window, you can set a local simulator and start it:
The Start button enables the corresponding Andriod simulator.
The trial version of Mono for Andriod can be developed and learned on any simulator. If you need to publish or deploy it to a real device, or publish it to the App Store, You need to purchase the relevant copyright, general Enterprise License fee $999, general professional license fee $399, see: https://store.xamarin.com/
Before developing a Mono for Andriod application, you must first understand the composition of Mono for Andriod:
The BCL of Mono for Andriod is based on the Silverlight and Client. NET3.5 programming sets. It includes a rich set:
1. Base Class Library
It mainly includes mscrolib. dll, System. dll, System. Core. dll, System. Data. dll, System. Json. dll, System. Runtime. Serialization. dll,
System. ServiceModel. dll, System. ServiceModel. Web. dll, System. Transactions. dll, System. Web. Services. dll,
System. Xml. dll, System. Xml. Linq. dll, etc.
2. Mono for Android Framework
It mainly includes Mono. Android. dll and Mono. Andriod. dll, as the core part of Andriod development, which are directly bound with the Andriod API.
3. Mono Libraries
It mainly includes Mono. xml. dll, Mono. Security. dll, Mono. Data. Tds. dll, Mono. Data. Sqlite. dll,
Mono. CompilerServices. SymbolWriter. dll, etc.
In addition, you can see an OpenTK. dll in the MonoAndriod Framework. It is an OpenGL/OpenAL encapsulation. It can be used to design 3D models and is quite useful in Andriod game development.
Activity and Activity Lifecycle
Activities, as the basic component of the Andriod application, share the same concept in mono or non-mono.
In the Andriod operating system, priority stacks are used to manage the operations of activities on devices. The lifecycle of an Activity is managed by the system in a unified manner. In android, Activity has four basic states:
1. Active/Runing: after a new Activity is started into the stack, it is at the front end of the screen and at the top of the stack. At this time, it is visible and can interact with users.
2. Paused: The status when the Activity is overwritten by another transparent or Dialog style Activity. At this time, it is still connected to the window manager, and the system continues to maintain its internal status, so it is still visible, but it has lost its focus, so it cannot interact with users.
3. Stopped: when the Activity is overwritten by another Activity and the loss of focus is invisible, it is in the Stopped state.
4. Killed: The Activity is Killed and recycled by the system or is in the Killed state when it is not started.
When an Activity instance is created, destroyed, or started, it is converted between the four States. This conversion relies on the action of the user program, illustrates the timing and conditions for Activity switching between different States (refer to: http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt ):
Android manages an Activity through an Activity stack. The status of an Activity Instance determines its position in the stack. The front-end Activity is always at the top of the stack. When the current Activity is destroyed due to exceptions or other reasons, the Activity on the second layer of the stack will be activated and go up to the top of the stack. When a new Activity is started into the stack, the original Activity is pushed to the second layer of the stack. The change in the position of an Activity in the stack reflects its transition between different States. Shows the relationship between the Activity status and its position in the stack:
As shown above, except for the Activity in the Active state at the top layer, other activities may be recycled when the system memory is insufficient. The more an Activity instance is at the bottom layer of the stack, it is more likely to be recycled by the system. The system is responsible for managing the instances of the Activity in the stack. It changes its position in the stack according to the Activity status.
Activity lifecycle call sequence:
Refer to the Code section of MonoAndriod:
[Activity (Label = "Hello M4A", MainLauncher = true)]
Public class Activity1: Activity
{
Protected override void OnCreate (Bundle bundle)
{
Base. OnCreate (bundle );
// Use UI created in Main. axml
SetContentView (Resource. Layout. Main );
Var aButton = FindViewById <Button> (Resource. Id. aButton );
Var aLabel = FindViewById <TextView> (Resource. Id. helloLabel );
AButton. Click + = (sender, e) =>{ aLabel. Text = "Hello from the button ";};
}
}
All the Activity pages are implemented based on the Activity (of course, the UI component of Fragment can also be implemented above Android 3.0), and the first construction of the Activity is completed through OnCreate.
Specific Activity lifecycle can also refer to the official documentation: http://docs.xamarin.com/android/tutorials/Activity_Lifecycle
Data communication between activities is implemented by using Intent classes. Intent classes are widely used in official demos. Different Intent types implement different functions, I will introduce this in later articles.
Finally, let's take an example to see how to create a Mono for Andriod application (take VS as an example ):
After you have installed everything about Mono for Andriod, we can see in the new project of:
Create an Andriod project. In the solution, we can see that a series of directories and files are generated:
Asserts contains files of any type in the application. The files contained here can be accessed through the Asserts class. In this case, you need to set the file generation operation to AndriodAsset:
You can access the file content through code, for example:
InputStream input = Assets. Open ("AboutAssets.txt ");
Resources contains application Resources, such as strings and images, and the declarative XML (Andriod XML) User Interface (which is put in Layout here. These Resources are accessed by generating the Resources class. You also need to set the file generation operation:
String. xml in Values is configured as a local resource String file, for example:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "helloButtonText"> Say Hello </string>
<String name = "helloLabelText"> Hello Mono for Android </string>
</Resources>
Access through code:
Var aLabel = new TextView (this );
ALabel. SetText (Resource. String. helloLabelText );
Var aButton = new Button (this );
AButton. SetText (Resource. String. helloButtonText );
Activity1.cs is a newly created implementation that inherits from the Activity class. Through this class, you can write the page-related logic.
Mono for andriodfinally packs codeas and resourcesfile into a .apk file, as shown in:
Write code:
[Activity (Label = "Hello M4A", MainLauncher = true)]
Public class Activity1: Activity
{
Protected override void OnCreate (Bundle bundle)
{
Base. OnCreate (bundle );
SetContentView (Resource. Layout. Main );
Var aButton = FindViewById <Button> (Resource. Id. aButton );
Var aLabel = FindViewById <TextView> (Resource. Id. helloLabel );
AButton. Click + = (sender, e) => {aLabel. Text = "Hi, Leepy! ";};
}
}
And Main. axml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/helloLabel"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/helloLabelText"/>
<Button
Android: id = "@ + id/aButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/helloButtonText"/>
</LinearLayout>
I will introduce the specific implementation in the next article.
To run the application, first enable an Android 2.2 simulator:
Debug the execution Program
Select the simulator you just started. (Note: When the application loads for the first time, it first installs the related mono Assembles and mono for andriod framework packages to the simulator, the packaged apk file will also be installed in the simulator, which takes several minutes)
Click "Say Hello" and "Li, Leepy" is displayed in the TextView control ".
This article ends first. The next article will focus on the features of Mono for Andriod and the use and practices of related APIs.