"Android" 12.2 Start and close activity with intent

Source: Internet
Author: User
<span id="Label3"></p><p><p>Category: C #, Android, VS2015;</p></p><p><p>Date created: 2016-02-23 I. INTRODUCTION</p></p><p><p>Android applications generally have multiple activity, in the activity, by calling the StartActivity method, and in the Method's parameters to pass the intent object, you can achieve different activity between the switch and data transfer.</p></p><p><p>When you start another activity by passing the intent object through the StartActivity method, you can split into two categories:</p></p><p><p>L explicit start: explicitly specify which activity is started in the created intent object;</p></p><p><p>L Implicit start: the Android system determines which activity should be started based on intent's actions and Data.</p></p><p><p>1. Explicitly start activity</p></p><p><p>When you explicitly start an activity by intent, you must first create a intent object and indicate the target component name to start in the constructor of the Object. For example:</p></p><p><p>var intent = new Intent (this, typeof (Activity1));</p></p><p><p>StartActivity (intent);</p></p><p><p>In this case, there is no other way to match the component name other than the intent Object.</p></p><p><p>2. Implicit activation activity</p></p><p><p>The implicit start activity is that the Android system automatically matches the corresponding intent according to the filtering rules, that is, it is not necessary to explicitly indicate which activity is started in the intent object, but to let the Android system decide who should start. In this case, the Android system automatically matches one or more of the activity that is best suited for processing intent. The matching activity may be the application itself, or it may be built into the Android system, or it may be provided by a 3rd party Application. therefore, the implicit launch of this approach emphasizes the reusability of components in Android Applications.</p></p><p><p>Note that if you want your program to show certain actions through activity, such as making a phone call, sending an email, texting, or using data from the Activity. At this point, first consider invoking the functionality provided by the system to implement (that is, the way the activity is implicitly started). In this case, you only need to specify the appropriate action by intent, which is where intent truly embodies its value. For example:</p></p><p><p>var call = new Intent (intent.actioncall); Initialize a phone call</p></p><p><p>Call. SetData (Android.Net.Uri.Parse ("tel:13811112222"));</p></p><p><p>StartActivity (call);</p></p><p><p>Here are some common action constants:</p></p><p><p></p></p> <p><p>Note: When you represent these Action constants in C #, type "Intent." Then select a Constant. These selectable constants remove the underscore of the delimited word and change the first letter of each word to uppercase, and the other letters to lowercase, for Example: Intent.actioncall.</p></p><p><p>In addition to specifying the appropriate action, you can also specify a uri, in which case Android invokes the built-in browser to implicitly start an Activity. For example:</p></p><p><p>Intent intent=new Intent (intent.actionview, uri.parse ("http://www.google.com");</p></p><p><p>StartActivity (intent);</p></p><p><p>3. Close Activity</p></p><p><p>Call finish () to terminate the Activity. You can also call Finishactivity () to terminate a separate activity that you started Earlier.</p></p><p><p>Remember: explicitly closing the activity is only for situations where you absolutely do not want the user to return to the activity Instance. In most cases, you should not explicitly call finish () or finishactivity () to close acitivity, but rather let the system manage it for YOU. second, Example-ch1201intentdemo</p></p><p><p>This example shows how to make a call through intent and how to start another Activity.</p></p><p><p>This example requires the application to have "call_phone" Permissions.</p></p><p><p>1. operation</p></p><p><p>Before running, add some contacts and their phone numbers in the emulator (the emulator itself has this function, just do it in the emulator), and then test the number you want to dial Out.</p></p><p><p>The image below is the main interface of the example, and the phone number dialed in the 2nd screen after clicking the "call" button is shown on the Right.</p></p><p><p></p></p> <p><p></p></p> <p><p>is to run after you click the go to Activity 1 Button.</p></p><p><p></p></p> <p><p>2. Main design steps</p></p><p><p>(1) Add "call_phone" permission</p></p><p><p>In solution explorer, Double-click the Project's "Properties" to enter the following interface, and then tick the "call_phone" option:</p></p> <p><p></p></p> <p><p>Once set, the following code is automatically added to the Androidmanifest.xml:</p></p><pre><pre><span style="color: #0000ff"><</span><span style="color: #800000"></span><span style="color: #ff0000">android:name</span><span style="color: #0000ff">= "android.permission.CALL_PHONE"</span><span style="color: #0000ff">/></span></pre></pre><p><p>(2) Add Ch1201_main.axml</p></p><p><p>Add the file under the Resources\layout folder, and the template selects "Layout":</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"><?</span></span><span style="color: #ff00ff"><span style="color: #ff00ff">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff"><span style="color: #0000ff">?></span></span><span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">LinearLayout</span></span><span style="color: #ff0000"><span style="color: #ff0000">xmlns:android</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:orientation</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "vertical"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Button</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:id</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@+id/btn1"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "wrap_content"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:text</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "call"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Button</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:id</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@+id/btn2"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "wrap_content"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:text</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "go to Activity 1"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span><span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">LinearLayout</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span></pre><p><p>(3) Add Ch1201_layout1.axml</p></p><p><p>Add the file under the Resources\layout folder, and the template selects "Layout":</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"><?</span></span><span style="color: #ff00ff"><span style="color: #ff00ff">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff"><span style="color: #0000ff">?></span></span><span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">LinearLayout</span></span><span style="color: #ff0000"><span style="color: #ff0000">xmlns:android</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:orientation</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "vertical"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">TextView</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:text</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "this is the interface of activity 1 \ n" prompt: Press "back" button to return "</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:textappearance</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "? android:attr/textappearancelarge"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "wrap_content"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "wrap_content"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:id</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@+id/textview1"</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:layout_gravity</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "center"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_margin</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "30dp"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span><span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">LinearLayout</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span></pre><p><p>(4) Add Ch1201IntentDemoMain.cs</p></p><p><p>Add the file under the Srcdemos folder, and the template selects "Activity":</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.app;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.content;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.os;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.widget;</span></span><span style="color: #0000ff"><span style="color: #0000ff">namespace</span></span><span style="color: #000000"><span style="color: #000000">mydemos.srcdemos{[Activity (Label</span></span>=<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">"example 12-1" Intent basic Usage 1</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">)] </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">ch1201intentdemomain:activity {</span></span><span style="color: #0000ff"><span style="color: #0000ff">protected</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Override</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">OnCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Base</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ch1201_Main); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Activate the phone dialing activity</span></span>Findviewbyid<button> (Resource.Id.btn1). Click + =<span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #0000ff"><span style="color: #0000ff">var</span></span>Call =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Intent (intent.actioncall); Call. SetData (Android.Net.Uri.Parse (</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">tel:13811112222</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">)); StartActivity (call); }; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">start a custom activity</span></span>Findviewbyid<button> (Resource.Id.btn2). Click + =<span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #0000ff"><span style="color: #0000ff">var</span></span>Intent =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Intent (<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span>,<span style="color: #0000ff"><span style="color: #0000ff">typeof</span></span><span style="color: #000000"><span style="color: #000000">(ch1201intentdemoactivity1)); StartActivity (intent); }; } }}</span></span></pre><p><p>(5) Add Ch1201IntentDemoActivity1.cs</p></p><p><p>Add the file under the Srcdemos folder, and the template selects "Activity":</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.app;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.os;</span></span><span style="color: #0000ff"><span style="color: #0000ff">namespace</span></span><span style="color: #000000"><span style="color: #000000">mydemos.srcdemos{[Activity (Label</span></span>=<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">"example 12-1" Intent basic Usage 1</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">)] </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">ch1201intentdemoactivity1:activity {</span></span><span style="color: #0000ff"><span style="color: #0000ff">protected</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Override</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">OnCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Base</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ch1201_Layout1); } }}</span></span></pre><p><p>Run.</p></p><p><p>"Android" 12.2 Start and close activity with intent</p></p></span>

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.