Android四大应用组件(一)——Activity

目录

一、基本理论

Activity概述

Intent概述

Intent的使用

Activity的使用

Activity的生命周期

TaskStack和lauchMode

设置监听的四种方式

二、应用练习示例

功能描述

需求分析

界面展示

代码示例

结果展示


一、基本理论

Activity概述

活动

四大应用组件之一

作用

提供能让用户操作并与之交互的界面

特点

1)它的类必须实现特定接口或继承特定类

2)需要在配置文件中配置其全类名

3)它的对象不是通过new来创建的, 而是系统自动创建的

4)它的对象具有一定的生命周期, 它的类中有对应的生命周期回调方法

Intent概述

意图

信使(Activity, Service, BroadcastReceiver三个组件间通信的信使)

分类:

1)显式: 操作当前应用自己的组件

2)隐式: 操作其它应用自己的组件

Intent的使用

1). 创建:

显式: Intent(Context context, Class activityClass)

隐式: Intent(String action) //与Activity与<intent-filter>的action匹配

2). 携带数据

额外: putExtra(String key, Xxx value) 内部用map容器保存

有特定前缀: setData(Uri data)  //tel:123123, smsto:123123

3). 读取数据:

额外: Xxx getXxxExtra(String key)

有特定前缀: Uri getData()

Activity的使用

1). 定义

a. 定义一个类 extends Activity, 并重写生命周期方法

b. 在功能清单文件中使用<activity>注册

2). 启动

a. 一般: startActivity(Intent intent)

b. 带回调启动: startActivityForResult(Intent intent, int requestCode)  并重写: onActivityResult(int requestCode, int resultCode, Intent data)

3). 结束

a. 一般: finish()

b. 带结果的: setResult(int resultCode, Intent data)

Activity的生命周期

1). Activity界面的状态:

运行状态: 可见也可操作

暂停状态: 可见但不可操作

停止状态: 不可见,但对象存在

死亡状态: 对象不存在

2). Activity的生命周期流程:

          

onCreate() : 加载布局和初始化的工作

onResume() : 只有经历此方法, 才进入运行状态

onDestroy() : 在对象死亡之前, 做一些收尾或清理的工作

TaskStack和lauchMode

TaskStack

1)在Android中,系统用Task Stack (Back Stack)结构来存储管理启动的Activity对象

2)一个应用启动,系统就会为其创建一个对应的Task Stack来存储并管理该应用的Activity对象

3)只有最上面的任务栈的栈顶的Activity才能显示在窗口中

lauchMode:

1)standard

标准模式,每次调用startActivity()方法就会产生一个新的实例。

2)singleTop

如果已经有一个实例位于Activity栈的顶部时,就不产生新的实例;如果不位于栈顶,会产生一个新的实例。

3)singleTask

只有一个实例, 默认在当前Task中

4)singleInstance

只有一个实例, 创建时会新建一个栈, 且此栈中不能有其它对象

设置监听的四种方式

1. layout中:   android:οnclick=“方法名”

                     Activity中: public void 方法名(View v) {   }

2. view.setOnclickListener(new View.OnclickListener(){})

3. view.setOnclickListener(this)

4. view.setOnclickListener(onclickListener成员变量)

 

二、应用练习示例

功能描述

1). 输入电话号, 点击"打电话", 进入拨号界面, 且已输入的指定的号码

2). 输入电话号, 长按"打电话", 直接打电话(进入拨打界面)

3). 输入电话和短信内容, 点击"发短信", 进入短信编辑界面, 且已有号码和内容

4). 输入电话和短信内容, 长按"发短信", 直接将短信发送给指定的手机号

需求分析

1. 界面布局
    1). 分析: 垂直的LinearLayout+水平的LinearLayout
    2). 编码
2. 在Activity中初始化需要操作的视图对象
3. 给button设置点击监听
4. 给button设置长按监听
5. 点击打电话进入拨号界面
    1). 创建一个Intent(隐式)
    2). 携带数据
    3). startActivity(intent)
6. 长按打电话进入打电话的界面(需要配置权限)
    1). 创建一个Intent(隐式)
    2). 携带数据
    3). startActivity(intent)    
7. 点击发短信进入短信编辑界面
    1). 创建一个Intent(隐式)
    2). 携带数据(号码/内容)
    3). startActivity(intent)    
8. 长按发短信直接发短信(需要配置权限)
    1). 得到SmsManager的对象
    2). 发送文本信息(短信)

界面展示

代码示例

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="电话号码:" />

        <EditText
            android:id="@+id/et_main_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:ems="10"
            android:hint="请输入手机号码" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="短信内容:" />

        <EditText
            android:id="@+id/et_main_sms"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:ems="10"
            android:hint="请输入短信内容" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="41dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/btn_main_call"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="打电话" />

        <Button
            android:id="@+id/btn_main_send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="发短信" />
    </LinearLayout>



</LinearLayout>

MainActivity.java

package com.example.app01_activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_main_number;
    private EditText et_main_sms;
    private Button btn_main_call;
    private Button btn_main_send;
    private View.OnLongClickListener OnLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (v == btn_main_call) {
//                Toast.makeText(MainActivity.this, "打电话长按事件", Toast.LENGTH_SHORT).show();
                //创建一个隐式intent对象
                Intent intent = new Intent(Intent.ACTION_CALL);
                //携带数据
                String number = et_main_number.getText().toString();
                intent.setData(Uri.parse("tel:" + number));
                //startActivity(intent)
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return false;
                }
                startActivity(intent);


            } else if (v == btn_main_send) {
//                Toast.makeText(MainActivity.this, "发短信长按事件", Toast.LENGTH_SHORT).show();
                //得到SmsManager的对象
                SmsManager smsManager = SmsManager.getDefault();
                //发送文本信息
                String number = et_main_number.getText().toString();
                String sms = et_main_sms.getText().toString();
                smsManager.sendTextMessage(number,null,sms,null,null);

            }
            //返回true表示此事件已经被消费了,不会再触发点击事件
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化视图对象
        et_main_number = findViewById(R.id.et_main_number);
        et_main_sms = findViewById(R.id.et_main_sms);
        btn_main_call = findViewById(R.id.btn_main_call);
        btn_main_send = findViewById(R.id.btn_main_send);
        //给视图对象设置点击监听
        btn_main_call.setOnClickListener(this);
        btn_main_send.setOnClickListener(this);
        //给视图对象设置长按监听
        btn_main_call.setOnLongClickListener(OnLongClickListener);
        btn_main_send.setOnLongClickListener(OnLongClickListener);
    }

    @Override
    public void onClick(View v) {
        if (v == btn_main_call) {
//            Toast.makeText(this,"打电话点击事件",Toast.LENGTH_SHORT).show();
            //创建一个隐式intent对象
//            05-13 18:14:19.695 1509-1880/system_process I/ActivityManager: Start proc 4325:com.android.dialer/u0a4 for activity com.android.dialer/.DialtactsActivity
            String action = "android.intent.action.DIAL";
            Intent intent = new Intent(action);
            //携带数据
            String number = et_main_number.getText().toString();
            intent.setData(Uri.parse("tel:"+number));
            //startActivity(intent)
            startActivity(intent);

        } else if (v == btn_main_send) {
//            Toast.makeText(this, "发短信点击事件", Toast.LENGTH_SHORT).show();
            //创建一个隐式intent对象
            String action = "android.intent.action.SENDTO";
            Intent intent = new Intent(action);
            //携带数据
            String number = et_main_number.getText().toString();
            intent.setData(Uri.parse("smsto:"+number));
            String sms = et_main_sms.getText().toString();
            intent.putExtra("sms_body",sms);
            //startActivity(intent)
            startActivity(intent);

        }
    }

}

结果展示

1). 输入电话号, 点击"打电话", 进入拨号界面, 且已输入的指定的号码

2). 输入电话号, 长按"打电话", 直接打电话(进入拨打界面)

3). 输入电话和短信内容, 点击"发短信", 进入短信编辑界面, 且已有号码和内容

4). 输入电话和短信内容, 长按"发短信", 直接将短信发送给指定的手机号

启动另外一个模拟器接收短信

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章