Video lesson: https://edu.csdn.net/course/play/7621
Contents of this chapter
Detailed Intent
Call system program
Use Intent to realize data transfer between components
Custom Intent
Detailed Intent
Intent is a run-time binding mechanism , which can connect two different components during program operation . Through Intent, your program can express a certain request or will to Android , and Android will select the appropriate component to complete the request according to the content of the will.
Android three basic components - the Activity, and Broadcast Receiver-- are through-Service through Intent activation mechanisms, different types of components have different delivery Intent manner . To activate a new Activity, or let an existing Activity do a new operation, you can start a new Service by calling Context.startActivity() or Activity.startActivityForResult() method, or pass to an existing Service New instructions, call the Context.startService() method or call the Context.bindService() method to bind the context object calling this method with the Service.
Intents are called intents, used to describe the actions, parameters, and additional data of an operation. They are used in many places and can be understood as similar to hyperlinks between web pages.
• View the information of a contact
• Send an email to someone
• call someone
Android will find the corresponding components according to the Intent and pass in the Intent for execution
Intent can be passed in during the following actions
• Content.startActivity ()
• Content.startService ()
• Content.sendBroadcast ()
Components in Android need to be registered in AndroidManifest.xml before they can be called
• Register through intent-filter
The composition of the intent, to transfer data between different activities, it is necessary to include the corresponding content in the intent, generally speaking, the most basic data should include:
• Action: used to specify what the action is to be implemented, such as ACTION_VIEW, ACTION_EDIT, etc.
• Data: specific data to be facts, generally represented by a Uri variable
• Category: A string that contains information about the type of component handling the intent. An intent object can have any number of categories. The intent class defines many category constants
• Type: Explicitly specify the data type of the Intent (MIME)
• component: Specify the class name of the target component of the Intent
• extras: additional information
Several common actions are as follows:
name |
description |
ACTION_CALL activity |
Start a call |
ACTION_EDIT activity |
Show user edited data |
ACTION_MAIN activity |
Start as the first Activity in the Task |
ACTION_SYNC activity |
Synchronize data on mobile phone and data server |
ACTION_BATTERY_LOW broadcast receiver |
Low battery warning |
ACTION_HEADSET_PLUG broadcast receiver |
Headphone plugging warning |
ACTION_SCREEN_ON broadcast receiver |
The screen turns bright warning |
ACTION_TIMEZONE_CHANGED broadcast receiver |
Change time zone warning |
Data attribute is used to match with Action
The attribute value is usually a string in URI format
The contents of the Data attribute corresponding to different Actions are also different
• The Data corresponding to ACTION_CALL usually starts with " tel: "
• The Data corresponding to ACTION_VIEW usually starts with " http: "
The value of the Data attribute can be set as follows
• setData() can only set URI
• setType() can only set MIME Type
• setDataAndType() can set URI or MIME Type
Category is used to describe the extended type information of the target component
Any description can be set
The Intent class defines several Category constants
•CATEGORY_BROWSABLE
•CATEGROY_HOME
•CATEGORY_LAUNCHER
Category属性的值可以通过如下方法设置
•addCategory()
•removeCategory()
Extras用于表达键值对的数据
•可以随意设置多对键值对
•这个属性与Android匹配Intent无关
•Intent提供了多个相对的get……方法和set……方法用于读写数据
•当传入到组件中时,通过Bundle的getExtras可以获得数据
<span style="font-family:SimSun;font-size:14px;">//发送MMSUri uri = Uri.parse("content://media/external/images/media/23");Intent it = new Intent(Intent.ACTION_SEND);it.putExtra("sms_body", "some text");it.putExtra(Intent.EXTRA_STREAM, uri);it.setType("image/png");startActivity(it);</span>
Intent的使用有两种形式
显示Intent:明确定义了目标组件的名称。通过指定具体的组件类,通知应用启动对应的组件。
隐式Intent:没有指定组件名称属性的Intent。通过属性和Intent-Filter进行匹配。
Intent的工作原理
1、调用者生成Intent对象,并设置相关属性
2、向Android提交Intent请求
3、Android对Intent进行解析,找到相应的组件执行
•这里可以匹配多个组件
•通过Intent-Filter匹配时,Android选择三个要素
Action
Data
Category
所有组件都通过在AndroidManifest.xml中的配置进行注册
•一个没有注册Intent-Filter的组件只能响应显式Intent请求
•以申明了Intent-Filter即可响应显式请求也可以响应隐式请求
使用Intent调用系统对象
<span style="font-family:SimSun;font-size:14px;">Uri uri = Uri.parse( "http://www.google.com");Intent it = new Intent(Intent.ACTION_VIEW,uri); </span>
显示地图
<span style="font-family:SimSun;font-size:14px;"> Uri uri = Uri.parse( "geo:38.899533,-77.036476" ); Intent it = new Intent(Intent.Action_VIEW,uri);</span>
调用拨号程序
<span style="font-family:SimSun;font-size:14px;"> Uri uri = Uri.parse( "tel:xxxxxx" ); Intent it = new Intent(Intent.Action_DIAL,uri);</span>
注意:拨打电话需要在配置文件中赋予权限
<span style="font-family:SimSun;font-size:14px;"> <uses-permission android:name="android.permission.CALL_PHONE"/></span>
使用Intent实现数据传递
在启动组件时,都需要通过Intent传递启动信息
Intent正是一个可以用来传递数据的媒介
<span style="font-family:SimSun;font-size:14px;">Intent it = new Intent(this, SettingActivity.class);it.putExtra(“username”, “root”);startActivity(it);</span>
在被启动的组件中,可以通过getIntent方法获得Intent对象
<span style="font-family:SimSun;font-size:14px;">Intent it = getIntent();String username = it.getExtras().getString(“username”);</span>
接收数据返回
被启动的组件结束时,需要将数据返回给调用者,通过Activity.startActivityForResult启动Activity
public void startActivityForResult (Intent intent, int requestCode)
•参数intent为启动Activity的意图描述
•参数requestCode为请求码,用于在回调时识别回调者
通过Activity.setResult设置返回信息
public void setResult (int resultCode, Intent data)
•参数resultCode为返回结果的结果编码,用于标识结果类型
•参数data为返回的数据
接收数据返回
启动者的启动调用示例
<span style="font-family:SimSun;font-size:14px;">startActivityForResult (intent, REQUEST_CODE);</span>
启动者的回调接收示例
<span style="font-family:SimSun;font-size:14px;">protected void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode == REQUEST_CODE) {if(resultCode == RESULT_OK) {data.getExtras().getString(“someresult”);}}}</span>
接收数据返回
被启动者设置回调结果示例
<span style="font-family:SimSun;font-size:14px;">Bundle bundle = new Bundle();bundle.putString(“someresult”, “somevalue”);bundle.putString(“otherresult”, “othervalue”);Intent it = new Intent();it.putExtra(bundle);setResult(RESULT_OK, it);finish();</span>
定制Intent和Intent-Filter
在需要启动另一个自定义项目中的Activity时
•由于另一个项目的Activity不属于当前项目,不能得到Class
•所以我们希望能够通过隐式Intent的方式启动
•这时就需要对Action等属性进行自定义
•并在Intent中使用这些自定义属性
Android中是允许Intent的自定义要求的
•首先在AndroidManifest.xml中使用Intent-Filter自定义各种属性
•然后在启动Activity时,在Intent中使用自定义属性