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

总结


目录
相关文章
|
2月前
|
XML Java Android开发
利用Bundle实现Android Activity间消息的传递
利用Bundle实现Android Activity间消息的传递
27 2
|
11天前
|
Android开发 开发者
Android UI设计中,Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等,定义在`styles.xml`。
【6月更文挑战第26天】Android UI设计中,Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等,定义在`styles.xml`。要更改主题,首先在该文件中创建新主题,如`MyAppTheme`,覆盖所需属性。然后,在`AndroidManifest.xml`中应用主题至应用或特定Activity。运行时切换主题可通过重新设置并重启Activity实现,或使用`setTheme`和`recreate()`方法。这允许开发者定制界面并与品牌指南匹配,或提供多主题选项。
19 6
|
12天前
|
Android开发 开发者
Android UI中的Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等。要更改主题
【6月更文挑战第25天】Android UI中的Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等。要更改主题,首先在`styles.xml`中定义新主题,如`MyAppTheme`,然后在`AndroidManifest.xml`中设置`android:theme`。可应用于全局或特定Activity。运行时切换主题需重置Activity,如通过`setTheme()`和`recreate()`方法。这允许开发者定制界面以匹配品牌或用户偏好。
15 2
|
23天前
|
Android开发
Android面试题之activity启动流程
该文探讨了Android应用启动和Activity管理服务(AMS)的工作原理。从Launcher启动应用开始,涉及Binder机制、AMS回调、进程创建、Application和Activity的生命周期。文中详细阐述了AMS处理流程,包括创建ClassLoader、加载APK、启动Activity的步骤,以及权限校验和启动模式判断。此外,还补充了activity启动流程中AMS的部分细节。欲了解更多内容,可关注公众号“AntDream”。
16 1
|
1月前
|
存储 Java Android开发
Android上在两个Activity之间传递Bitmap对象
Android上在两个Activity之间传递Bitmap对象
17 2
|
9天前
|
Android开发 UED 开发者
Android Activity启动模式详解
Android Activity启动模式详解
13 0
|
9天前
|
Android开发 UED
Android Activity的生命周期详解
Android Activity的生命周期详解
9 0
|
9天前
|
Android开发
Android Activity跳转详解
Android Activity跳转详解
14 0
|
1月前
|
Java Android开发 C++
42. 【Android教程】活动:Activity
42. 【Android教程】活动:Activity
22 2
|
2月前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
30 3