Explicit and implicit Activity activation for Android Learning

Source: Internet
Author: User

Explicit and implicit Activity activation for Android Learning
Activity_main contains the following four button copy Code <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. example. twoactivity. mainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "I am the first interface"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "Jump to the Second Interface"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click2" android: text = "activate an application of the system"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click3" android: text =" Implicit intent jump to the Second Interface "/> <Button android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: onClick =" click4 "android: text = "activation text message interface"/> </LinearLayout> copy code acivity_two.xml copy Code <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientatio N = "vertical" tools: context = "com. example. twoactivity. mainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" I am the second interface "/> <ImageView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/resize"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_conten T "android: onClick =" click "android: text = "Jump to the Second Interface"/> </LinearLayout> copy the code. Now we need to click "Jump to the second page" in main ", for the program to jump to the two page, we will create two classes under src to inherit the MainActivity class respectively. java copy code package com. example. twoactivity; import android. annotation. suppressLint; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; public Class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // when the user clicks the button, the page jumps to the second page. public void click (View view) {// click the button to activate the second page, redirect // Intent/** intent Intent = new intent (); Intent. setClassName (this, * "com. example. twoactivity. otherScreenActivity "); // sets the component to be activated for this intent * StartActivity (intent); */Intent intent = new Intent (this, OtherScreenActivity. class); startActivity (intent);} public void click2 (View view) {// click the button to activate the second interface and jump to // Intent intent = new Intent (); intent. setClassName ("com. android. gallery "," com. android. camera. galleryPicker "); // sets the startActivity (intent) component to be activated );} /*** use implicit intent to activate the third interface ***/@ SuppressLint ("NewApi") public void click3 (View View) {// click the button to activate the second interface. // Intent intent = new Intent (); intent. setAction ("com. example. xxx "); intent. addCategory ("android. intent. category. DEFAULT "); // specify the Data Type // if the data type does not exist // intent. setType ("vnd. android. cursor. item/haha "); // if no data type exists // intent. setData (Uri. parse ("itheima: gagaga"); // if there are types and data with intent. setDataAndTypeAndNormalize (Uri. parse ("itheima: gagaga"), "vnd. android. cursor. item/ Haha "); startActivity (intent); // Action Data // hitting people, making soy sauce // making tea, soaking girls // soaking green tea, soaking red dust, bubble Oolong // addCategory additional information}/*** activate SMS service ***/public void click4 (View view) {Intent intent = new Intent (); intent. setAction ("adnroid. intent. action. SENDTO "); intent. addCategory ("android. intent. category. DEFAULT "); intent. setData (Uri. parse ("sms: 110"); startActivity (intent) ;}} copy the code OtherScreenActivity. java copy code package com.example.tw Oactivity; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. widget. toast;/*** activity is one of the important components of the system, you must configure ***/public class OtherScreenActivity extends Activity {// rewrite the Oncreate method of activity @ Override protected void onCreate (Bundle savedInstanceState) in the configuration file) {// TODO Auto-generated method stub super. onCreate (sav EdInstanceState); setContentView (R. layout. activity_two); Intent intent = getIntent (); // gets the intent Uri uri = Intent. getData (); String result = uri. toString (); Toast. makeText (this, "data is:" + result, 0 ). show () ;}} copies the code and AndroidManifest in the light file. xml copy Code <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. example. ce "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "android: targetSdkVersion = "21"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <activity android: name = ". resultActivity "android: label =" @ string/app_name "> </activity> </application> </manifest> copy the code

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.