Android 學習之顯式激活與隱式激活Activity

activity_main裡面有如下四個按鈕

 

復制代碼

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

    xmlns:tools="https://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="我是第一個界面" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click"

        android:text="跳轉到第二個界面" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click2"

        android:text="激活系統的一個應用" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click3"

        android:text="隱式意圖跳轉到第二個界面" />

    

     <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click4"

        android:text="激活短信的界面" />

 

</LinearLayout>

復制代碼

 

 

acivity_two.xml

 

復制代碼

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

    xmlns:tools="https://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="我是第二個界面" />

 

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/abc_menu_dropdown_panel_holo_dark" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click"

        android:text="跳轉到第二個界面" />

 

</LinearLayout>

復制代碼

 

 

現在我們需要點擊main裡面的"跳轉到第二個頁面",讓程序顯示地跳轉到two頁面裡面

 

下面我們在src下面建立兩個類,分別繼承Activity類

 

MainActivity.java

 

復制代碼

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);

    }

 

    // 當用戶點擊按鈕的時候跳轉到第二個界面

    public void click(View view) {

        // 單擊按鈕激活第二個界面,跳轉

        // Intent 意圖

        /*

         * Intent intent = new Intent(); intent.setClassName(this,

         * "com.example.twoactivity.OtherScreenActivity");// 設置這個意圖要激活的組件

         * startActivity(intent);

         */

        Intent intent = new Intent(this, OtherScreenActivity.class);

        startActivity(intent);

    }

 

    public void click2(View view) {

        // 單擊按鈕激活第二個界面,跳轉

        // Intent 意圖

 

        Intent intent = new Intent();

        intent.setClassName("com.android.gallery",

                "com.android.camera.GalleryPicker");// 設置這個意圖要激活的組件

        startActivity(intent);

 

    }

 

    /**

     * 采用隱式意圖激活第三個界面

     * 

     * */

    @SuppressLint("NewApi")

    public void click3(View view) {

        // 單擊按鈕激活第二個界面,跳轉

        // Intent 意圖

 

        Intent intent = new Intent();

        intent.setAction("com.example.xxx");

        intent.addCategory("android.intent.category.DEFAULT");

 

        // 指定數據類型

        // 如果隻要類型沒有數據

        // intent.setType("vnd.android.cursor.item/haha");

        // 如果隻要數據沒有類型

        // intent.setData(Uri.parse("itheima:gagaga"));

 

        // 如果有類型和數據都有

        intent.setDataAndTypeAndNormalize(Uri.parse("itheima:gagaga"),

                "vnd.android.cursor.item/haha");

        startActivity(intent);

        // 動作數據

        // 打人,打醬油

        // 泡茶,泡妞

        // 泡綠茶,泡紅塵,泡烏龍

        // addCategory附加信息

 

    }

 

    /**

     * 激活短信的服務

     * 

     * */

    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);

    }

}

復制代碼

OtherScreenActivity.java

 

復制代碼

package com.example.twoactivity;

 

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.widget.Toast;

 

/**

 * activity是系統的重要組件之一 操作系統要想找到activity,就必須在清單文件裡面配置

 * 

 * */

public class OtherScreenActivity extends Activity {

    // 重寫activity的Oncreate方法

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_two);

 

        Intent intent = getIntent();// 獲取到激活他的意圖

        Uri uri = intent.getData();

        String result = uri.toString();

        Toast.makeText(this, "數據是:" + result, 0).show();

    }

 

}

復制代碼

並且在清淡文件裡面AndroidManifest.xml

 

復制代碼

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="https://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>

復制代碼

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *