Android 中Activity和Intent的详解

Posted 路宇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
            
        
    

总结

以上是关于Android 中Activity和Intent的详解的主要内容,如果未能解决你的问题,请参考以下文章

Android之Intent和Activity

Android 中Activity和Intent的详解

Android——Activity和Intent及Activity的生命周期

Android-----Intent中通过startActivity(Intent intent )隐式启动新的Activity

android 中如何使用intent通过传递变量传递数据

android开发中service如何接收activity用intent发送来的数据