Android应用程序中的多个Activity的显示创建和调用



布局文件:

<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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="openActivity"
        android:text="开启第二个Activity" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="openSystemActivity"
        android:text="开启系统的Activity" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="checkConnection"
        android:text="检测网络状态" />

</LinearLayout>
主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;

/**
 * 案例演示的是显示的激活Activity
 * 
 * @author MartinDong
 * 
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	/**
	 * 用户想要打开第二个界面的时候
	 * 
	 * @param view
	 */
	public void openActivity(View view) {
		// 创建意图对象
		Intent intent = new Intent();
		// 方便调用setComponent与一个明确的类名。
		// 相当于创建了一个新的组件
		// 会话位置|指定要激活的具体的Activity
		intent.setClassName(this, "com.examp.manyactivity.SecondActivity");
		// 第二种方式,是在创建意图对象的时候进行指定Activity
		// Intent intent2 = new Intent(this, SecondActivity.class);

		// 激活一个Activity
		startActivity(intent);
	}

	/**
	 * 开启系统中的Activity<br>
	 * 案例演示的是开启图库的Activity
	 * 
	 * @param view
	 */
	public void openSystemActivity(View view) {

		/*
		 * 05-31 07:42:44.581: I/ActivityManager(150): 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);

	}

	/**
	 * 检测网路状态
	 * 
	 * @param view
	 */
	public void checkConnection(View view) {
		/*
		 * 05-31 08:03:01.848: I/ActivityManager(150): START
		 * {act=android.intent.action.MAIN cmp=com.android.settings/.SubSettings
		 * (has extras) u=0} from pid 306 由于这里4.0的网络的管理需要传入附加数据,本功能使用2.3的虚拟机<br>
		 * 05-31 08:05:47.072: I/ActivityManager(61): Starting: Intent {
		 * act=android.intent.action.MAIN
		 * cmp=com.android.settings/.WirelessSettings } from pid 168
		 */
		// 检测网路的连接状态
		// 创建连接管理对象
		ConnectivityManager cm = (ConnectivityManager) this
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// 需要的权限 android.Manifest.permission.ACCESS_NETWORK_STATE
		// 获取网络的连接信息
		NetworkInfo info = cm.getActiveNetworkInfo();
		// 如果没有任何的网络信息info为null;
		if (info != null && info.isConnected()) {
			Toast.makeText(this, "网络可用......", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "网不可用,请设置......", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent();
			intent.setClassName("com.android.settings",
					"com.android.settings.WirelessSettings");
			startActivity(intent);
		}

	}
}

第二个Activity文件:

package com.examp.manyactivity;

import android.app.Activity;
import android.os.Bundle;

/**
 * 自定义的Activity<br>
 * 必须要继承Activity<br>
 * Activity是系统的四大组件之一<br>
 * 操作系统想要找到Activity就必须在清单文件AndroidManifest.xml进行注册<br>
 * 
 * 
 * @author MartinDong
 * 
 */
public class SecondActivity extends Activity {

	/**
	 * 一般都会重写的方法,用途大都是初始化一些数据,和程序的界面<br>
	 * Activity创建的时候进行调用
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置显示的布局
		setContentView(R.layout.activity_tow);

	}

}

第二个Activity对应的布局文件:

<?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_content"
        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_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="246dp"
        android:layout_height="match_parent" />

</LinearLayout>

清单文件的配置:

<?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="18" />

    <!-- icon:指定应用程序的图标;label:指定应用程序的名称; -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Activity的注册 -->
        <!-- 如果Activity不进行icon,label的设置,那么将默认的使用应用application的icon,label 的设置 -->
        <!-- name指定的是布局文件对应的Activity类 -->
        <activity
            android:name="com.examp.manyactivity.MainActivity"
            android:label="@string/app_name" >

            <!--  -->
            <intent-filter>

                <!-- 告诉Android的系统这是应用的主界面 -->
                <action android:name="android.intent.action.MAIN" />
                <!-- 告诉Android的系统创建一个应用图标 -->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.examp.manyactivity.SecondActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

注:本案例的网络查看状态只能在2.3的模拟器上使用;


Demo源码下载:

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








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值