Android 中Activity和Intent的详解

简介: Android 中Activity和Intent的详解

Activity的理解

Activity的运行状态分为

四种状态对应的生命周期流程图如下

Intent的理解如下

Intent的分类如下

IntentFilter的理解

Activity和Intent相关的API如下

Activity的Task Stack(任务栈)

特点:后进先出

Activity的LauchMode(加载模式)

具体详解看这篇文章

Android 中Activity的四种启动模式

应用实例进行打电话与发短信,隐示意图的练习,代码如下

1、布局页面activity_intent_action.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".IntentActionActivity">
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话号码:"
            android:textSize="15sp" />
        <EditText
            android:id="@+id/edit_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入电话号码"
            android:textSize="15sp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayout_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearLayout"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="短信内容:"
            android:textSize="15sp" />
        <EditText
            android:id="@+id/edit_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入短信内容"
            android:textSize="15sp" />
    </LinearLayout>
    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearLayout_message"
        android:text="打电话" />
    <Button
        android:id="@+id/btn_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearLayout_message"
        android:layout_toRightOf="@id/btn_call"
        android:text="发短信" />
</RelativeLayout>

2、在AndroidManifest.xml中声明打电话与发送短信的权限,这两个权限属于危险权限,还要在Java代码中动态申请。

<!-- 拨打电话的权限 -->
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
  <!--发送短信的权限-->
    <uses-permission android:name="android.permission.SEND_SMS" />

3、在IntentActionActivity类中进行具体代码的实现

public class IntentActionActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
    private EditText edit_phone;
    private EditText edit_message;
    private Button btn_call;
    private Button btn_message;
    private static final int CALL_PHONE_CODE = 0X11;
    private static final int SEND_SMS_CODE = 0X22;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_action);
        edit_phone = findViewById(R.id.edit_phone);
        edit_message = findViewById(R.id.edit_message);
        btn_call = findViewById(R.id.btn_call);
        btn_message = findViewById(R.id.btn_message);
        btn_call.setOnClickListener(this);
        btn_message.setOnClickListener(this);
        btn_call.setOnLongClickListener(this);
        btn_message.setOnLongClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_call: //点击打电话 进入拨号的界面
                //1.创建一个隐式意图Intent
                String action = "android.intent.action.DIAL";
//                action=Intent.ACTION_DIAL;//一样的
                Intent intent = new Intent(action);
                //2.携带数据
                String number = edit_phone.getText().toString();
//                  <data android:scheme="tel"/>
                intent.setData(Uri.parse("tel:" + number));
                startActivity(intent);
                break;
            case R.id.btn_message://点击发 短信进入编辑短信界面
                Intent intent1 = new Intent(Intent.ACTION_SENDTO);
                //携带号码和内容
                String numbers = edit_phone.getText().toString();
                String sms = edit_message.getText().toString();
                intent1.setData(Uri.parse("smsto:" + numbers));
                intent1.putExtra("sms_body", sms);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }
    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.btn_call: //长按打电话进入打电话的界面
                //请求打电话的权限并打电话
                RequestPermission(CALL_PHONE_CODE);
                break;
            case R.id.btn_message://长按发送短信
                //请求发信息的权限并 发送短信信息
                RequestPermission(SEND_SMS_CODE);
                break;
            default:
                break;
        }
        return true;
    }
    public void CallPage() {
        String action = "android.intent.action.CALL";
        Intent intent = new Intent(action);
        //2.携带数据
        String number = edit_phone.getText().toString();
        intent.setData(Uri.parse("tel:" + number));
        startActivity(intent);
    }
    public void sendText() {
        //1.得到SmsManager的对象
        SmsManager smsManager = SmsManager.getDefault();
        //2.发送文本信息(短信)
        String message = edit_message.getText().toString();
        String phone = edit_phone.getText().toString();
        smsManager.sendTextMessage(phone, null, message, null, null);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            switch (requestCode) {
                case CALL_PHONE_CODE:
                    CallPage();
                    break;
                case SEND_SMS_CODE:
                    sendText();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                case CALL_PHONE_CODE:
                    Toast.makeText(this, "打电话权限请求失败!", Toast.LENGTH_SHORT).show();
                    break;
                case SEND_SMS_CODE:
                    Toast.makeText(this, "发送短信权限请求失败!", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(this, "测试!", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
    private void RequestPermission(int code) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            switch (code) {
                case CALL_PHONE_CODE:
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_CODE);
                    } else {
                        CallPage();
                    }
                    break;
                case SEND_SMS_CODE:
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_CODE);
                    } else {
                        sendText();
                    }
                    break;
                default:
                    break;
            }
        }
    }
}

总结


目录
相关文章
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
44 1
|
3月前
|
Android开发 开发者
Android基础知识:什么是Intent?有哪些类型的Intent?
Android基础知识:什么是Intent?有哪些类型的Intent?
57 0
|
3月前
|
数据库 Android开发 开发者
Android基础知识:请解释Activity的生命周期。
Android基础知识:请解释Activity的生命周期。
45 2
|
6月前
|
存储 SQL 人工智能
Android Activity启动流程一:从Intent到Activity创建
Android Activity启动流程一:从Intent到Activity创建
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
293 54
|
4月前
|
XML 安全 Java
Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)
Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)
36 0
|
4月前
|
Android开发
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
46 0
|
4月前
|
Android开发
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
43 0
|
5月前
|
Android开发
安卓activity管理器
安卓activity管理器
27 0