Intent用法总结

33 篇文章 2 订阅
15 篇文章 2 订阅

Intent用法总结

1、Intent的作用

Intent是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由 Intent来协助完成 Android各个组件之间的通讯。比如说调用startActivity()来启动一个Activity,或者由broadcaseIntent()来传递给所有感兴趣的BroadcaseReceiver,再或者由startService() / bindservice()来启动一个后台的 service。所以可以看出来,Intent 主要是用来启动其他的 activity 或者 service,所以可以将 intent 理解成 activity 之间的粘合剂。

Intent作用的表现形式为:

  • 启动Activity

    通过Context.startActvity() / Activity.startActivityForResult()启动一个Activity;

  • 启动Service
    通过Context.startService()启动一个服务,或者通过Context.bindService()和后台服务交互;

  • 发送Broadcast
    通过广播方法Context.sendBroadcasts() / Context.sendOrderedBroadcast() / Context.sendStickyBroadcast()发给Broadcast Receivers

2、Intent属性

Intent对象大致包括7大属性:Action(动作)Data(数据)Category(类别)Type(数据类型)Component(组件)Extra(扩展信息)Flag(标志位)。其中最常用的是Action属性和Data属性。

  • **Action:**用来表现意图的行动
    一个字符串变量,可以用来指定Intent要执行的动作类别。常见的action有:

    Activity Actions

    类型作用
    ACTION_MAIN表示程序的入口
    ACTION_VIEW自动以最合适的方式显示Data
    ACTION_EDIT提供可以编辑的
    ACTION_PICK选择一个一条Data,并且返回它
    ACTION_DAIL显示Data指向的号码在拨号界面Dailer上
    ACTION_CALL拨打Data指向的号码
    ACTION_SEND发送Data到指定的地方
    ACTION_SENDTO发送多组Data到指定的地方
    ACTION_RUN运行Data,不管Data是什么
    ACTION_SEARCH执行搜索
    ACTION_WEB_SEARCH执行网上搜索
    ACRION_SYNC执同步一个Data
    ACTION_INSERT添加一个空的项到容器中

    Broadcast Actions:

    类型作用
    ACTION_TIME_TICK当前时间改变,并即时发送时间,只能通过系统发送。调用格式"android.intent.action.TIME_TICK"
    ACTION_TIME_CHENGED设置时间。调用格式"android.intent.action.TIME_SET"
  • **Data:**表示与动作要操纵的数据

    一个URI对象是一个引用的data的表现形式,或是data的MIME类型;data的类型由Intent的action决定。

  • **Category:**用来表现动作的类别

    一个包含Intent额外信息的字符串,表示哪种类型的组件来处理这个Intent。任何数量的Category 描述都可以添加到Intent中,但是很多intent不需要category,下面列举一些常用的category:

    类型作用
    CATEGORY_DEFAULT把一个组件Component设为可被implicit启动的
    CATEGORY_LAUNCHER把一个action设置为在顶级执行。并且包含这个属性的Activity所定义的icon将取代application中定义的icon
    CATEGORY_BROWSABLE当Intent指向网络相关时,必须要添加这个类别
    CATEGORY_HOME使Intent指向Home界面
    CATEGORY_PREFERENCE定义的Activity是一个偏好面板Preference Panel
  • **Type:**指定数据类型

    一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

  • **Component:**目的组件

    指定Intent的目标组件名称,当指定了这个属性后,系统将跳过匹配其他属性,而直接匹配这个属性来启动对应的组件。

    ComponentName

    组件名称 目标组件的全类名

    setComponent() setClass() setClassName() getComponent()

  • **Extra:**扩展信息

    Intent可以携带的额外 key-value 数据,你可以通过调用putExtra()方法设置数据,每一个 key对应一个 value数据。你也可以通过创建 Bundle对象来存储所有数据,然后通过调用putExtras()方法来设置数据。

    类型作用
    EXTRA_BCC存放邮件密送人地址的字符串数组
    EXTRA_CC存放邮件抄送人地址的字符串数组
    EXTRA_EMAIL存放邮件地址的字符串数组
    EXTRA_SUBJECT存放邮件主题字符串
    EXTRA_TEXT存放邮件内容
    EXTRA_KEY_EVENT以KeyEvent对象方式存放触发Intent 的按键
    EXTRA_PHONE_ NUMBER存放调用ACTION_CALL 时的电话号码
  • **Flag:**期望这个意图的运行模式

    用来指示系统如何启动一个Activity,可以通过setFlags()或者addFlags()可以把标签flag用在Intent中。

    类型作用
    FLAG_ACTIVITY_CLEAR_TOP相当于SingleTask
    FLAGE_ACTIVITY_SINGLE_TOP相当于SingleTop
    FLAG_ACTIVITY_NEW_TASK类似于SingleInstance
    FLAG_ACTIVITY_NO_HISTORY当离开该Activity后,该Activity将被从任务栈中移除

3、Intent种类

  • 显式Intent
    显式,即直接指定需要打开的activity对应的类。

    1)构造方法传入Component,最常用的方式:

    Intent intent = new Intent(this, SecondActivity.class);  
    startActivity(intent);  
    

    2)setComponent 方法

    ComponentName componentName = new ComponentName(this, SecondActivity.class);  
    // 或 ComponentName componentName = new ComponentName(this, "com.example.app.SecondActivity");  
    // 或 ComponentName componentName = new ComponentName(this.getPackageName(), "com.example.app.SecondActivity");  
      
    Intent intent = new Intent();  
    intent.setComponent(componentName);  
    startActivity(intent);
    

    3)setClass / setClassName 方法

    Intent intent = new Intent();    
    intent.setClass(this, SecondActivity.class);  
    // 或 intent.setClassName(this, "com.example.app.SecondActivity");  
    // 或 intent.setClassName(this.getPackageName(),"com.example.app.SecondActivity");            
    
    startActivity(intent);  
    

    显式Intent通过Component可以直接设置需要调用的Activity类,可以唯一确定一个Activity,意图特别明确,所以是显式的。设置这个类的方式可以是Class对象(如SecondActivity.class),也可以是包名加类名的字符串(如"com.example.app.SecondActivity")。

  • 隐式Intent

    隐式,不明确指定启动哪个Activity,而是设置Action、Data、Category,让系统来筛选出合适的Activity。筛选是根据所有的<intent-filter>来筛选。

    下面以Action为例:
    AndroidManifest.xml文件中,首先被调用的Activity要有一个带有<intent-filter>并且包含<action>的Activity,设定它能处理的Intent,并且category设为"android.intent.category.DEFAULT"。action的name是一个字符串,可以自定义,例如这里设成为"mark"

    <activity  
        android:name="com.example.app.SecondActivity">  
        <intent-filter>  
            <action android:name="mark"/>  
            <category android:name="android.intent.category.DEFAULT"/>  
        </intent-filter>  
    </activity>  
    

    然后,在MainActivity,才可以通过这个action name找到上面的Activity。下面两种方式分别通过setAction和构造方法方法设置Action,两种方式效果相同。
    1)setAction 方法

    Intent intent = new Intent();  
    intent.setAction("mark");  
    startActivity(intent);  
    

    2)构造方法直接设置 Action

    Intent intent = new Intent("mark");  
    startActivity(intent);  
    

    为了防止应用程序之间互相影响,一般命名方式是包名+Action名,例如这里命名"mark"就很不合理了,就应该改成"com.example.app.Test"

    隐式启动的实例

    • intentfilterdemo项目中进行定义一个布局文件,布局文件中有两个按钮

      <?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">
      
          <Button
              android:id="@+id/button01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="隐式启动"
              android:onClick="onClick"/>
      
          <Button
              android:id="@+id/button02"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/button01"
              android:layout_below="@+id/button01"
              android:text="隐式启动其他项目的Activity"
              android:onClick="onClick"/>
      
      </RelativeLayout>
      
    • MainActivity中获取两个按钮,并进行隐式启动

      package com.tinno.intentfilterdemo;
      
      import androidx.appcompat.app.AppCompatActivity;
      
      import android.content.Intent;
      import android.net.Uri;
      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 onClick(View view){
              switch (view.getId()){
                  case R.id.button01: //点击按钮以隐式启动方式启动
                      Intent intent = new Intent();
                      intent.setAction("com.yztc.action");
                      //intent.setData(Uri.parse("yztc://com:90/res"));
                      //intent.setDataAndType(Uri.parse("yztc://com:90/res"),"text/**");
                      startActivity(intent);
                      break;
                  case R.id.button02: //启动其他项目的Activity
                      Intent intent1 = new Intent();
                      intent1.setAction("com.yztc.intent");
                      startActivity(intent1);
                      break;
              }
          }
      }
      
    • button01是本项目的隐式启动,在AndroidManifest.xml中对OtherActivity进行配置

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.tinno.intentfilterdemo">
      
          <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.TencentClass">
              <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>
      
              <activity android:name=".OtherActivity"
                  android:exported="true">
                  <intent-filter>
                      <action android:name="com.yztc.demo"/>
                      <action android:name="com.yztc.action"/>
                      <!-- 默认分类 -->
                      <category android:name="android.intent.category.DEFAULT"/>
                      <!-- scheme://host:port/path  yztc://com:90/res-->
                      <!--<data android:scheme="yztc" android:host="com" android:port="90" android:path="/res" android:mimeType="text"/>-->
                  </intent-filter>
              </activity>
          </application>
      
      </manifest>
      
    • button02是启动另一个项目的 Activity,在另一个项目 intentdemoAndroidManifest.xml进行ResultActivity配置

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.tinno.intentdemo">
      
          <!-- 添加拨打电话的权限 -->
          <uses-permission android:name="android.permission.CALL_PHONE"/>
          <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.TencentClass">
              <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>
              <activity android:name=".ResultActivity"
                  android:exported="true">
                  <intent-filter>
                      <action android:name="com.yztc.intent"/>
                      <action android:name="com.yztc"/>
                      <category android:name="android.intent.category.DEFAULT"/>
                  </intent-filter>
              </activity>
          </application>
      
      </manifest>
      

4、Intent用法

  • 调用拨号程序
// 调用拨打电话,给10010拨打电话
Uri uri = Uri.parse("tel:10010");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
// 直接拨打电话,需要加上权限 <uses-permission id="android.permission.CALL_PHONE" /> 
Uri callUri = Uri.parse("tel:10010"); 
Intent intent = new Intent(Intent.ACTION_CALL, callUri); 
  • 发送短信或彩信
 // 给10010发送内容为“Hello”的短信
Uri uri = Uri.parse("smsto:10010");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
startActivity(intent);
// 发送彩信(相当于发送带附件的短信)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hello");
Uri uri = Uri.parse("content://media/external/images/media/23");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
  • 通过浏览器打开网页
// 打开百度主页
Uri uri = Uri.parse("https://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
  • 发送电子邮件
// 给someone@domain.com发邮件
Uri uri = Uri.parse("mailto:someone@domain.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
// 给someone@domain.com发邮件发送内容为“Hello”的邮件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, "someone@domain.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.setType("text/plain");
startActivity(intent);
// 给多人发邮件
Intent intent=new Intent(Intent.ACTION_SEND);
String[] tos = {"1@abc.com", "2@abc.com"}; // 收件人
String[] ccs = {"3@abc.com", "4@abc.com"}; // 抄送
String[] bccs = {"5@abc.com", "6@abc.com"}; // 密送
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.setType("message/rfc822");
startActivity(intent);
  • 显示地图与路径规划
 // 打开Google地图中国北京位置(北纬39.9,东经116.3)
Uri uri = Uri.parse("geo:39.9,116.3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
// 路径规划:从北京某地(北纬39.9,东经116.3)到上海某地(北纬31.2,东经121.4)
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
  • 播放多媒体
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/foo.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
  • 选择图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 2);
  • 拍照
 // 打开拍照程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
 // 取出照片数据
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
  • 获取并剪切图片
// 获取并剪切图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop", "true"); // 开启剪切
intent.putExtra("aspectX", 1); // 剪切的宽高比为1:2
intent.putExtra("aspectY", 2);
intent.putExtra("outputX", 20); // 保存图片的宽和高
intent.putExtra("outputY", 40);
intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路径
intent.putExtra("outputFormat", "JPEG");// 返回格式
startActivityForResult(intent, 0);
// 剪切特定图片
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));
intent.putExtra("outputX", 1); // 剪切的宽高比为1:2
intent.putExtra("outputY", 2);
intent.putExtra("aspectX", 20); // 保存图片的宽和高
intent.putExtra("aspectY", 40);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));
startActivityForResult(intent, 0);
  • 打开手机应用市场
// 打开手机应用市场,直接进入该程序的详细页面
Uri uri = Uri.parse("market://details?id=" + packageName);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
  • 安装程序
String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";   
Intent intent = new Intent(Intent.ACTION_VIEW);   
intent.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");   
startActivity(intent);  
  • 卸载程序
Uri uri = Uri.parse("package:" + packageName);   
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
  • 进入设置界面
// 进入系统设置界面
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivity(intent);
  • 实例

    package com.tinno.intentdemo;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    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(View view){
            switch (view.getId()){
                case R.id.btn_start:    //点击按钮启动Activity
                    /*Intent intent = new Intent();
                    ComponentName component = new ComponentName(MainActivity.this,ResultActivity.class);
                    intent.setComponent(component);*/
    
                    /*Intent intent = new Intent();
                    intent.setClass(this,ResultActivity.class);*/
    
                    Intent intent = new Intent();
                    intent.setClassName(this,ResultActivity.class.getName());
                    startActivity(intent);
                    break;
                case R.id.btn_call: //拨打电话
                    //Uri.parse(String s) uri抽象类,调用静态的parse()方法 指定action操作的数据
                    Intent intent2 = new Intent(Intent.ACTION_CALL, Uri.parse("tel://10000"));
                    startActivity(intent2);
                    break;
                case R.id.btn_send: //发送短信
                    Intent intent3 = new Intent(Intent.ACTION_SENDTO,Uri.parse("sms://10000"));
                    startActivity(intent3);
                    break;
                case R.id.btn_openView: //打开网页
                    Intent intent4 = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
                    startActivity(intent4);
                    break;
                case R.id.btn_play: //播放音频
                    Intent intent5 = new Intent(Intent.ACTION_VIEW);
                    intent5.setDataAndType(Uri.parse("file:///"+ Environment.getExternalStorageDirectory().getAbsolutePath()+"Far.mp3"),"audio/*");
                    startActivity(intent5);
                    break;
            }
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值