一、intent的用途
Intent的基本用途:启动 Activity、启动服务和传递广播。
二、intent的属性
intent大致共有七种属性:Component、Action、Category、Data、Type、Extra、Flag。
1. Component属性:
指定Intent的component属性的启动方式称为显式intent,没有指定component属性为隐式intent。
class intentDemo1Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_intent_demo1)
val bt = findViewById<Button>(R.id.test_button)
bt.setOnClickListener {
val comp = ComponentName(this@intentDemo1Activity,SecondActivity::class.java)
val intent = Intent()
intent.component = comp
startActivity(intent)
}
}
}
2. Action和Category属性:
(1) 一个Intent的对象只能设置一个action属性,但可以设置多个Category属性。
(2) 不能单独使用Category属性,必须配合Action或Data使用。
(3) intent设置的多个Category属性,intent-filter都能满足才可以。
(4) <intent-filter>中若设置categroy规则,则必须先设置android.intent.category.DEFAULT
过滤规则如下:
<intent-filter>
<action android:name="android.intent.action.HELLO"></action>
<action android:name="android.intent.action.WORLD"></action>
<category android:name="android.intent.category.HELLO"></category>
<category android:name="android.intent.category.WORD"></category>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
//错误使用,必须先设置data或action属性
val intent = Intent()
intent.addCategory("android.intent.category.HELLO")
startActivity(intent)
//报错,第二条category不满足
val intent = Intent()
intent.action = "android.intent.action.HELLO"
intent.addCategory("android.intent.category.HELLO")
intent.addCategory("android.intent.category.ABC")
startActivity(intent)
3. 指定Action和Category调用系统Activity
android内部提供了大量Action和Category常量,用于启动系统程序组件。
如查看并获取联系人电话号码
在androidManifest.xml文件中添加
<uses-permission android:name="android.permission.READ_CONTACTS"/>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".intentDemo.intentDemo1Activity">
<TextView
android:id="@+id/show_tel_name"
android:paddingHorizontal="5dp"
android:paddingVertical="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="">
</TextView>
<TextView
android:id="@+id/show_tel_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="5dp"
android:paddingVertical="10dp"
android:text="">
</TextView>
<Button
android:id="@+id/show_tel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮"
>
</Button>
</LinearLayout>
activity
class intentDemo1Activity : AppCompatActivity() {
companion object{
const val PICK_CONTACT = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_intent_demo1)
val bt = findViewById<Button>(R.id.show_tel_button)
bt.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(this@intentDemo1Activity.checkSelfPermission(android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED){
Log.i("read_contacts","已经授权")
//请求系统联系人目录
requestContact()
}
else{
this@intentDemo1Activity.requestPermissions(arrayOf(android.Manifest.permission.READ_CONTACTS),0x100)
}
}
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode== 0x100){
if(permissions.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
requestContact()
}
}
}
fun requestContact(){
val intent = Intent()
intent.action = Intent.ACTION_PICK
intent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
startActivityForResult(intent, PICK_CONTACT)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
PICK_CONTACT -> {
if(resultCode == Activity.RESULT_OK){
val contactData = data?.data
if(contactData!=null){
val cur = CursorLoader(this,contactData,null,null,null,null)
val cursor = cur.loadInBackground()
if(cursor?.moveToFirst() == true){
val contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))
var phoneNumber = "电话号码为空"
val phones = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,"${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = $contactId",null,null)
if(phones?.moveToFirst()==true){
phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
)
}
phones?.close()
val tvName = findViewById<TextView>(R.id.show_tel_name)
val tvPhone = findViewById<TextView>(R.id.show_tel_number)
tvName.text = name
tvPhone.text = phoneNumber
}
}
}
}
}
}
}
4. Data、Type属性与intent-filter配置
Data和Type属性会相互覆盖,如果先设置Data属性,然后设置Type属性,那么Data属性就会覆盖掉,反之亦然。如果想要同时设置两个属性,则可以使用setDataAndType()方法。
Data属性通常接受一个Uri对象,对象的形式如下:
scheme://host:post/path
在AndroidManifest.xml文件中为组件声明Data、Type属性。
<data android:mimeType="" <!--声明该组件所能匹配的Intent的Type属性-->
android:scheme="" <!--用于声明Data属性的scheme部分-->
android:host="" <!--用于声明Data属性的host部分-->
android:port="" <!--用于声明Data属性的port部分-->
android:path="" <!--用于声明Data属性的path部分-->
android:pathPrefix="" <!--用于该组件所能匹配的Intent的Data属性的path前缀-->
android:pathPatern=""/> <!--用于该组件所能匹配的Intent的Data属性的path字符串模板-->
(1) 如果目标组件的<data/>的属性只指定了scheme属性,那么只要Intent的Data属性的scheme部分与该组件相同,就可启动该组件。
(2) 如果目标组件的<data/>的属性只指定了scheme、host属性,那么只要Intent的Data属性的scheme、host部分与该组件相同,就可启动该组件。
(3) 如果目标组件的<data/>的属性只指定了scheme、host、port属性,那么只要Intent的Data属性的scheme、host、port部分与该组件相同,就可启动该组件。
(4) 如果目标组件的<data/>子元素只指定了scheme、host、path属性,那么只要Intent的Data属性的scheme、host、path部分与该组件相同,就可启动该组件。
(5) 如果目标组件的<data/>子元素只指定了scheme、host、post、path属性,那么只要Intent的Data属性的scheme、host、post、path部分与该组件相同,就可启动该组件。
(6) 如果目标组件的<data/>的属性只指定了port属性,没有指定host属性,那么port属性将不会生效。
(7) 如果目标组件的<data/>的属性只指定了path属性,没有指定host属性,那么path属性将不会起作用。
5. 使用Action和Data属性启动系统Activity
打开网页
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.data = Uri.parse("http://www.baidu.com")
startActivity(intent)
拨打电话
val intent = Intent()
intent.action = Intent.ACTION_DIAL
intent.data = Uri.parse("tel:1352664455")
startActivity(intent)
6. Extra属性
Extra用来携带数据,以key-value方式进行存储,可以在不同activity之间进行数据交换 ,通过putExtra()方法进行数据传入。
7.Flag属性
Intent的flag属性用于该Intent添加一些额外的控制旗标,使用addFlags()方法进行添加。
常用的Flag有:
FLAG_ACTIVITY_BROUGHT_TO_FRONT: 如果该activity已经在后台,则将该activity移动到前台。
FLAG_ACTIVITY_CLEAR_TOP:Activity已经在后台,则将处于该Activity之前的Activity全部弹出Activity栈。
FLAG_ACTIVITY_NEW_TASK: 默认Flag属性,创建一个新的Activity。
FLAG_ACTIVITY_NO_ANIMATION: 控制要启动的Activity不使用过度动画
FLAG_ACTIVITY_NO_HISTORY: 该Activity不会存储在栈中
更多查看官网Api