Display creation and invocation of multiple activity in an Android application

Source: Internet
Author: User



Layout file:

<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:orien tation= "Vertical" tools:context= ".        Mainactivity "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:text= "@string/hello_world"/> <button android:layout_width= "Match_parent" Android:la        yout_height= "Wrap_content" android:onclick= "openactivity" android:text= "open second activity"/> <button Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:onclick= "openSystemAc Tivity "android:text=" Open the system activity "/> <button android:layout_width=" Match_parent "Android: layout_height= "Wrap_content" android:onclick= "checkconnection" android:text= "Detect network Status"/></linearlayout& Gt
code for the main activity
Package Com.examp.manyactivity;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.os.bundle;import Android.view.view;import android.widget.toast;/** * Case demo is the activation activity displayed * * @author Martindong * */public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {s Uper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} /** * User wants to open the second interface when * * @param view */public void openactivity (view view) {//Create intent Object Intent Intent = new intent ();//Convenient to call SetComponent with a clear class name. Equivalent to creating a new component//session Location | Specifies the detailed activityintent.setclassname (this, "com.examp.manyactivity.SecondActivity") to be activated;// Another way to do this is to specify activity//Intent intent2 = new Intent (this, secondactivity.class) when creating the intent object;//Activating a activitystartactivity (intent);} /** * Open the activity<br> in the system * Case demo is to open the library activity * * @param view */public void opensystemactivity (view view) {/* * 05-31 07:42:44.581:i/activitymanager: START * {Act=android.intent.action.main * cat=[ Android.intent.category.LAUNCHER] flg=0x10200000 * Cmp=com.android.gallery/com.android.camera.gallerypicker u=0} from * pid 282 */intent Intent = new Intent (); Intent.setclassname ("Com.android.gallery", " Com.android.camera.GalleryPicker "); StartActivity (intent);} /** * Detect Network status * * @param view */public void checkconnection (view view) {/* * 05-31 08:03:01.848:i/activitymanager ($): S Tart * {Act=android.intent.action.main cmp=com.android.settings/. Subsettings * (have extras) u=0} from PID 306 because 4.0 of the network management needs to pass in additional data, this feature uses 2.3 of the virtual machine <br> * 05-31 08:05:47.072:i/activity Manager: starting:intent {* Act=android.intent.action.main * cmp=com.android.settings/. Wirelesssettings} from PID 168 *///Check the connection status of the network//Create connection management Object Connectivitymanager cm = (Connectivitymanager) this.getsystemser Vice (context.connectivity_service);//required Permissions for Android. manifest.permission.access_network_state//getting connection information for a network networkinfo info =Cm.getactivenetworkinfo ();//Assume that no matter what the network information info for null;if (info! = null && info.isconnected ()) {Toast.maketext ( This, "Network available ...", Toast.length_short). Show (); else {Toast.maketext (this, "Network not available, set ...", Toast.length_short). Show (); Intent Intent = new Intent (); Intent.setclassname ("Com.android.settings", "com.android.settings.WirelessSettings"); StartActivity (intent);}}

A second activity file:

Package Com.examp.manyactivity;import Android.app.activity;import android.os.bundle;/** * self-defined activity<br> * Must inherit activity<br> * activity is one of the four components of the system <br> * operating system to find the activity, you must register it in the manifest file Androidmanifest.xml <br > * * *  @author martindong *  */public class Secondactivity extends Activity {/** * Generally overrides the method, mostly to initialize some data, and program Interface <br> * Call when activity is created */@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate);//Set the displayed layout Setcontentview (R.layout.activity_tow);}}

The second activity corresponds to the layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <ratingbar android:id= "@+id/ratingbar1" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content"/> <ratingbar android:id= "@+id/ratingbar2" android:layout_width= "Wrap_conten        T "android:layout_height=" wrap_content "/> <checkedtextview android:id=" @+id/checkedtextview1 " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "CheckedTextView"/ > <spinner android:id= "@+id/spinner1" android:layout_width= "Match_parent" Android:layout_hei ght= "Wrap_content"/> <progressbar android:id= "@+id/progressbar1" style= "? Android:attr/progressbars Tylehorizontal "Android:layoUt_width= "Wrap_content" android:layout_height= "wrap_content"/> <progressbar android:id= "@+id/progr EssBar2 "style="? Android:attr/progressbarstylelarge "Android:layout_width=" 246DP "Android:layout_heig ht= "Match_parent"/></linearlayout>

Configuration of the manifest file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.examp.manyactivity "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <!--icon: Specifies the application's icons; Label: Specifies the name of the application; --<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <application androi D:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:th Eme= "@style/apptheme" > <!--activity-<!--assuming that the activity does not icon,label settings, the app will be used by default Applicati On the Icon,label settings--<!--name specifies the corresponding activity class for the layout file--<activity android:name= "COM.E Xamp.manyactivity.MainActivity "android:label=" @string/app_name "> <!----> &lt ;intent-filter> <!--Tell Android system This is the app's main interface--<action android:name= "Android.intent.action.MAIN"/> <!            --Tell the Android system to create an app icon--<category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= "Com.examp.manyactivity.Seco Ndactivity "android:label=" @string/app_name "> </activity> </application></manifest >

Note: The Network view status of this case can only be used on the simulator of 2.3;


Demo Source code Download:

http://download.csdn.net/detail/u011936142/7429455








Display creation and invocation of multiple activity in an Android application

Related Article

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.