最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。

      Activity作为Android的四大组件之一;有关于其概念,你可以点击查看(概念1概念2);我这里不会展开,因为我今天会主要结合Intent(意图)实现Activity之间跳转与数据传送展开介绍。

1,Intent介绍

意图是Android程序中各组件之间交互的一种主要方式,主要分为下面两类:

  • 显示Intent
  • 隐式Intent

在下面的代码中,我们都会使用到,上面也有很清晰的注释;

2,示例代码

FirstActivity.java代码:

package com.hfut.activityskiptest;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class FirstActivity extends AppCompatActivity {

    Intent explicitIntent;
    Intent implicitIntent;
    Intent forResultIntent;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        textView=findViewById(R.id.first_tv);
//        //隐藏actionBar
//        ActionBar actionBar = getActionBar();
//        if (actionBar != null) {
//            actionBar.hide();
//        }
    }

    /**
     * 显示意图开启服务
     *
     */
    public void explicitSkip(View view) {
        //显示意图
        explicitIntent = new Intent(this, SecondActivity.class);
//        //添加数据方式1
//        explicitIntent.putExtra("name", "why");
//        explicitIntent.putExtra("age", 26);

        //添加数据方式1
        explicitIntent.putExtra("data",textView.getText().toString());

        startActivity(explicitIntent);

    }


    /**
     * 隐式意图开启服务
     *
     */
    public void implicitSkip(View view) {
        //隐式意图用法一
//        implicitIntent = new Intent("com.hfut.activityskiptest.why");

        //隐式意图用法二字系统意图Action

//        implicitIntent=new Intent(Intent.ACTION_DIAL);
//        implicitIntent.setData(Uri.parse("tel:10010"));
        implicitIntent=new Intent(Intent.ACTION_VIEW);
        implicitIntent.setData(Uri.parse(""));
        startActivity(implicitIntent);
    }


    /**
     * forResult开启方式,获取回传数据,配合onActivityResult使用
     *
     */
    public void startForResult(View view){

        forResultIntent = new Intent(this,FourthActivity.class);
        //第一个参数:意图;第二个参数:请求码,唯一即可
        startActivityForResult(forResultIntent,1);

    }

    /**
     * 为了获取回传数据:
     * @param requestCode:请求码
     * @param resultCode:结果状态码
     * @param intent:意图数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        textView.append("第四页回传数据如下:\n");
        textView.append("    "+intent.getStringExtra("FourthPageData"));
    }
}

SecondActivity.java代码:

package com.hfut.activityskiptest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

Intent intent;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//获取传过来的intent,也就是打开该页面时带过来的
intent = getIntent();
textView = findViewById(R.id.Second_tv_data);

}

public void getData(View view) {

//获取数据2
// textView.append("name:" + intent.getStringExtra("name") + "\n");
// textView.append("age:" + intent.getIntExtra("age", 0));

//获取数据2
textView.append("第一页从第四页回传数据如下:\n");
textView.append(" "+intent.getStringExtra("data"));

}
}

ThirdActivity.java代码:

package com.hfut.activityskiptest;

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;

 public class ThirdActivity extends AppCompatActivity {

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

 }

 }

FourthActivity.java代码:

package com.hfut.activityskiptest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class FourthActivity extends AppCompatActivity {
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fourth);
        editText=findViewById(R.id.fourth_et);
    }

    public void sendData(View view){
        //配置需要回传的数据
        String data=editText.getText().toString();
        Intent intent=new Intent();
        if(!(data.equals(null)||data.equals(""))){
            intent.putExtra("FourthPageData", data);
            setResult(RESULT_OK, intent);
        }
        else{
            intent.putExtra("FourthPageData", "没有回传任何数据");
            setResult(RESULT_OK, intent);
        }
        finish();
    }
}

activity_first.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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.hfut.activityskiptest.FirstActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:text="@string/firstPage"
        android:textColor="#DC143C"
        android:textSize="25dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="explicitSkip"
        android:text="@string/explicitSkip"
        android:textSize="25dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="implicitSkip"
        android:text="@string/implicitSkip"
        android:textSize="25dp" />

    <Button
        android:onClick="startForResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/callBackStartActivity"
        android:textSize="25dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp">

        <TextView
            android:id="@+id/first_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:hint="@string/displayCallBackData"
            android:textSize="20dp" />
    </ScrollView>

</LinearLayout>

activity_second代码:

<?xml version="1.0" encoding="utf-8"?>
<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.hfut.activityskiptest.SecondActivity">

    <TextView

        android:gravity="center_horizontal"
        android:textColor="#DC143C"
        android:textSize="25dp"
        android:layout_marginTop="30dp"
        android:text="@string/secondPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_marginTop="15dp"
        android:onClick="getData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/getData"
        android:textSize="20dp"/>

    <TextView
        android:layout_marginTop="15dp"
        android:textSize="15dp"
        android:id="@+id/Second_tv_data"
        android:hint="显示从第一页获取信息"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_third.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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.hfut.activityskiptest.ThirdActivity">

    <TextView
        android:gravity="center_horizontal"
        android:textColor="#DC143C"
        android:textSize="25dp"
        android:layout_marginTop="30dp"
        android:text="@string/thirdPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_fourth.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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.hfut.activityskiptest.ThirdActivity">

    <TextView
        android:gravity="center_horizontal"
        android:textColor="#DC143C"
        android:textSize="25dp"
        android:layout_marginTop="30dp"
        android:text="@string/fourthPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:textSize="25dp"
        android:layout_marginTop="15dp"
        android:onClick="sendData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sendData"/>

    <ScrollView
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:layout_marginTop="10dp"
            android:id="@+id/fourth_et"
            android:textSize="20dp"
            android:hint="@string/callBackData"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </ScrollView>

</LinearLayout>

主配置文件AndroidManifests.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hfut.activityskiptest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity" />
        <activity android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="com.hfut.activityskiptest.why" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".FourthActivity"></activity>
    </application>

</manifest>

还有values下面的strings.xml代码:

<resources>
    <string name="app_name">ActivitySkipTest</string>
    <string name="buttonIntroduction">点击上面对应按钮测试</string>
    <string name="explicitSkip">显示跳转到第二页</string>
    <string name="implicitSkip">隐式跳转到第三页</string>

    <string name="firstPage">当前是第一页</string>
    <string name="secondPage">当前是第二页</string>
    <string name="thirdPage">当前是第三页</string>
    <string name="fourthPage">当前是第四页</string>

    <string name="getData">获取第一页传来的数据</string>
    <string name="sendData">回传数据给第一页</string>

    <string name="callBackData">请在此处编辑回传数据,然后点击进行数据回传</string>
    <string name="callBackStartActivity">获取回传数据开启活动方式</string>
    <string name="displayCallBackData">此处显示第四页面回传数据</string>

</resources>

3,运行结果

第一步:运行程序

                                 

android第一行 android第一行代码第二版pdf_显示Intent

                                                                                              图 1

第二步:点击“获取回传数据开启活动方式”按钮

                                 

android第一行 android第一行代码第二版pdf_显示Intent_02

                                                                                               图 2

第三步:编辑EditText里面文本信息

                                  

android第一行 android第一行代码第二版pdf_隐式Intent_03

                                                                                               图 3

第四步:点击“回传数据给第一页”

                                  

android第一行 android第一行代码第二版pdf_android第一行_04

                                                                                                图 4

第五步:点击“显示跳转到第二页”

                                 

android第一行 android第一行代码第二版pdf_android第一行_05

                                                                                                 图 5

第六步:返回主界面,点击“隐式跳转到第三页”

                            

android第一行 android第一行代码第二版pdf_隐式Intent_06

                                                                                                     图 6

 

总结:我在Demo里面通过注释进行了显示Intent和隐式Intent实现组件之间跳转与传输数据(这里只有活动);并且,使用了一个系统的Action实现打开一个网页的目的,我们还可以实现拨打电话等等功能,之间可以研究一下系统的Action有哪些;