Android 学习之显式激活与隐式激活Activity

来源:互联网 发布:java读取json文件 编辑:程序博客网 时间:2024/05/17 23:15

在res界面里面有两个布局文件activity_main和acivity_two

activity_main里面有如下四个按钮

复制代码
<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="我是第一个界面" />    <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="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="我是第二个界面" />    <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="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>
复制代码
0 0