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 对象设置动作:

Copy Highlighter-hljs
public Intent setAction(String action)

使用 satData() 方法可以为 Intent 对象设置动作:

Copy Highlighter-hljs
public Intent setData(android.net.Uri data)

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

Copy Highlighter-hljs
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 样例#

浏览器打开百度网页#

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

打开拨号页面#

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

直接拨打电话#

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

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

直接拨打电话的代码如下:

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

打开短信页面#

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

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

打开短信页面的代码如下:

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

给指定的人发送短信#

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

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

给指定的人发送短信的代码如下:

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

播放文件中的音乐#

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

卸载程序#

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

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

卸载程序的代码如下:

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

安装程序#

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

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

安装程序有 2 种方法,第一种是使用 ACTION_VIEW,指定 apk 包的路径安装。

Copy Highlighter-hljs
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,通过包名匹配安装。

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

完整代码#

AndroidManifest.xml#

Copy Highlighter-hljs
<?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#

Copy Highlighter-hljs
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#

Copy Highlighter-hljs
<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 移动应用开发》,杨谊 主编、喻德旷 副主编,人民邮电出版社

posted @   乌漆WhiteMoon  阅读(1295)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-10-15 sqli-labs 通关指南:Less 9、10
点击右上角即可分享
微信分享提示
CONTENTS