Android:隱式 Intent 調用標准 Action


隱式 Intent

隱式 Intent 調用過程根據動作名稱及可處理的數據類型,找到匹配的 Activity 組件,並解析所攜帶的數據 URI 完成對數據(消息)的處理。

Intent 主要有以下 6 個重要屬性。

屬性 說明
Componentname(目標組件名稱) 包含包名稱和類名稱,Componentname 被設置時就顯式指定了要轉向的組件,否則需要根據其他信息進行篩選查找
Action(動作) 字符串,代表系統定義的常用的動作
Data(數據) 通常是 UR I格式定義的操作數據。
Category(類別) 指定當前 Action 被執行的環境,android.intent.category.LAUNCHER 表示啟動程序時就將被執行,默認值為CATEGORY_DEFAULT
Extras(附加) 傳遞額外數據
Flags(標記) 指示 Android 如何啟動目標 Activity

隱式 Intent 對象定義

使用隱式 Activity 時,需要為 Intent 對象定義 Action、Category 和 Data 數學,然后使用 startActivity() 方法就能調用對應的 Activity。使用 satAction() 方法可以為 Intent 對象設置動作:

public Intent setAction(String action)

使用 satData() 方法可以為 Intent 對象設置動作:

public Intent setData(android.net.Uri data)

其中 Data 使用的是 android.net.Uri 類型,使用 Uri.parse() 把字符串解析為 Uri 類型。

public static Uri parse(String uriString)

Action 字符串常量

Action 常量 對應字符串 說明
ACTION_MAIN android.intent.action.MAIN 應用程序入口
ACTION_VIEW android.intent.action.VIEW 顯示指定數據
ACTION_ATTACH_DAT Aandroid.intent.action.ATTACH DATA 指定某塊數據將被附加到其他地方
ACTION_EDIT android.intent.action.EDIT 編輯指定數據
ACTION_PICK android.intent.action.PICK 從列表中選擇某項並返回所選的數據
ACTION_CHOOSER android.intent.action.CHOOSER 顯示一個 Activity 選擇器
ACTION_GET_CONTENT android.intent.action.GET_CONTENT 讓用戶選擇數據,並返回所選數據
ACTION_DIAL android.intent.action.DIAL 顯示撥號面板
ACTION_CALL android.intent.action.CALL 直接向指定用戶打電話
ACTION_SEND android.intent.action.SEND 向其他人發送數據
ACTION_SENDTO android.intent.action.SENDTO 向其他人發送消息
ACTION_ANSWER android.intent.action.ANSWER 應答電話
ACTION_INSERT android.intent.action.INSERT 插入數據
ACTION_DELETE android.intent.action.DELETE 刪除數據
ACTION_RUN android.intent.action.RUN 運行數據
ACTION_SYNC android.intent.action.SYNC 執行數據同步
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 用於選擇 Activity
ACTION_SEARCH android.intent.action.SEARCH 執行搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 執行 Web 搜索

調用標准 Action 樣例

瀏覽器打開百度網頁

uri = Uri.parse("http://www.baidu.com");
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

打開撥號頁面

intent.setAction(Intent.ACTION_DIAL);         
intent.setData(Uri.parse("tel:12345678910"));
startActivity(intent);

直接撥打電話

安裝程序首先要在 AndroidManifest.xml 文件中添加 CALL_PHONE 權限:

<uses-permission android:name="android.permission.CALL_PHONE"/>

直接撥打電話的代碼如下:

intent.setAction(Intent.ACTION_CALL);              
intent.setData(Uri.parse("tel:12345678910"));
startActivity(intent);

打開短信頁面

安裝程序首先要在 AndroidManifest.xml 文件中添加 READ_SMS 權限:

<uses-permission android:name="android.permission.READ_SMS"/>

打開短信頁面的代碼如下:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);

給指定的人發送短信

安裝程序首先要在 AndroidManifest.xml 文件中添加 SEND_SMS 權限:

<uses-permission android:name="android.permission.SEND_SMS"/>

給指定的人發送短信的代碼如下:

intent = new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:12345678910"));
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);

播放文件中的音樂

intent = new Intent(Intent.ACTION_VIEW);
uri = Uri.parse("file:///sdcard/誤入迷失森林.mp3");
intent.setDataAndType(uri, "audio/*");
startActivity(intent);

卸載程序

安裝程序首先要在 AndroidManifest.xml 文件中添加 REQUEST_DELETE_PACKAGES 權限:

<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>

卸載程序的代碼如下:

intent = new Intent(Intent.ACTION_DELETE);
uri = Uri.parse("package:程序的包名");
intent.setData(uri);
startActivity(intent);

安裝程序

安裝程序首先要在 AndroidManifest.xml 文件中添加 REQUEST_INSTALL_PACKAGES 權限:

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

安裝程序有 2 種方法,第一種是使用 ACTION_VIEW,指定 apk 包的路徑安裝。

intent = new Intent(Intent.ACTION_VIEW);
uri = Uri.fromFile(new File("/文件路徑/程序名.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);


第二種是使用 ACTION_INSTALL_PACKAGE,通過包名匹配安裝。

uri = Uri.fromParts("package", "com.example.myapplication3", null);
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE, uri);
startActivity(intent);

完整代碼

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication3">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
</manifest>

MainActivity

package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.os.StrictMode;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    Intent intent = new Intent();
    private Uri uri = null;

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

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.detectFileUriExposure();
        }
    }

    @SuppressWarnings("deprecation")
    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                // 打開百度瀏覽器
                uri = Uri.parse("http://www.baidu.com");
                intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                break;

            case R.id.button2:
                // 打電話
                // Intent.ACTION_DIAL 跳到撥號界面
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:12345678910"));
                startActivity(intent);
                break;

            case R.id.button3:
                // 打電話
                // Intent.ACTION_CALL 直接打電話
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:12345678910"));
                startActivity(intent);
                break;

            case R.id.button4:
                // 打開短信頁面
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setType("vnd.android-dir/mms-sms");
                startActivity(intent);
                break;

            case R.id.button5:
                // 發送短信
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("smsto:12345678910"));
                intent.putExtra("sms_body", "The SMS text");
                startActivity(intent);
                break;

            case R.id.button6:
                // 播放音樂
                intent = new Intent(Intent.ACTION_VIEW);
                uri = Uri.parse("file:///sdcard/誤入迷失森林.mp3");
                intent.setDataAndType(uri, "audio/*");
                startActivity(intent);
                break;

            case R.id.button7:
                // 卸載程序
                intent = new Intent(Intent.ACTION_DELETE);
                uri = Uri.parse("package:com.example.myapplication3");
                intent.setData(uri);
                startActivity(intent);
                break;

            case R.id.button8:
                // 安裝程序
                intent = new Intent(Intent.ACTION_VIEW);
                uri = Uri.fromFile(new File("/sdcard/app-debug.apk"));
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
                startActivity(intent);
                break;

            case R.id.button9:
                // 安裝程序2
                uri = Uri.fromParts("package", "com.example.myapplication3", null);
                intent = new Intent(Intent.ACTION_INSTALL_PACKAGE, uri);
                startActivity(intent);
            default:
                break;
        }
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button1"
        android:onClick="doClick"
        android:text="打開指定網頁" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button2"
        android:onClick="doClick"
        android:text="打開撥號面板"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button3"
        android:onClick="doClick"
        android:text="直接撥打指定號碼" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button4"
        android:onClick="doClick"
        android:text="打開發短信的界面" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button5"
        android:onClick="doClick"
        android:text="給指定的人發短信" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button6"
        android:onClick="doClick"
        android:text="播放指定路徑的音樂" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button7"
        android:onClick="doClick"
        android:text="卸載程序" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button8"
        android:onClick="doClick"
        android:text="安裝程序" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/button9"
        android:onClick="doClick"
        android:text="安裝程序2" />
</LinearLayout>

參考資料

《零基礎學 Android》,明日科技編著,吉林大學出版社
《Android 移動應用開發》,楊誼 主編、喻德曠 副主編,人民郵電出版社


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM