Android Studio下Intent显式/隐式启动,发短信,打电话,访问网页

目标:完成相应Activity的跳转。
1.通过显式/隐式跳转到下一个界面
2.实现打电话、发短信、打开浏览器功能
效果:
在这里插入图片描述
在这里插入图片描述

Activity的生命周期如下图

在这里插入图片描述
1.目录
在这里插入图片描述
2.activity_main.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=".MainActivity">
    
        <TextView
            android:id="@+id/head1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="自定义跳转!"
            />
    
        <Button
            android:id="@+id/button_explicit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/head1"
            android:layout_margin="10dp"
            android:onClick="click_explicit"
            android:text="显式跳转"/>
    
        <Button
            android:id="@+id/button_implicit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button_explicit"
            android:layout_margin="5dp"
            android:onClick="click_implicit"
            android:text="隐式跳转"/>
    
        <TextView
            android:id="@+id/head2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button_implicit"
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:text="系统工具跳转!"
            />
    
        <Button
            android:id="@+id/button_call"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/head2"
            android:layout_margin="10dp"
            android:onClick="click_call"
            android:text="打电话"/>
    
        <Button
            android:id="@+id/button_browser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button_call"
            android:layout_margin="5dp"
            android:onClick="click_browser"
            android:text="打开浏览器"/>
    
        <Button
            android:id="@+id/button_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button_browser"
            android:layout_margin="10dp"
            android:onClick="click_message"
            android:text="发短信"/>
    
    </RelativeLayout>

2.MainActivity.java

  package cn.qjnu.wxf;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    //显式跳转
    public void click_explicit(View view) {
        Intent intent = new Intent(this,secondActivity.class);
        startActivity(intent);
    }

    //隐式跳转
    public void click_implicit(View view) {
        Intent intent = new Intent();
        intent.setAction("cn.qjnu.activeity.THIRD_ACTIVITY");
        startActivity(intent);
    }

    //打电话
    public void click_call(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:10086"));
        startActivity(intent);
    }

    //浏览器
    public void click_browser(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://www.baidu.com"));
        startActivity(intent);
    }

    //发短信
    public void click_message(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("smsto:10086"));
        intent.putExtra("sms_body","The SMS text");
        startActivity(intent);
    }
}

3.AndroidManifest.xml
在这里插入图片描述
4.其他页面可以不做任何修改,但是为了能够辨别不同的页面,你可以自己添加一下文字标识。

最后对以上的部分代码解释:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
如果有问题留言我们一起讨论!

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