13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

详细分析IntentFilter的匹配规则

前言

日常的Android开发中,我们会用到IntentFilter的匹配规则。IntentFilter的主要规则分为action、category、data三个类别,只有完美匹配才能成功启动目标Activity;

今天我们就来讲解下;

一、Activity的调用模式

Activity的调用模式有两种:显式调用和隐式调用;

1、显式调用

大多数情况下我们最常接触到的就是显式调用了:

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

其实严格来讲,这个也不算是显式调用,因为在显式调用的意义中需要明确之处被启动的对象的组件信息,包括包名和类名,这里并没有之处包名:

 
 
 
 
  1. Intent intent = new Intent(Intent.ACTION_MAIN);
  2. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  3. ComponentName cn = new ComponentName("com.test","com.test.MainActivity");
  4. intent.setComponent(cn);
  5. startActivity(intent);

2、隐式调用

需要Intent能匹配目标组件的IntentFilter中所设置的过滤信息.如果不匹配将无法启动目标Activity;

 
 
 
 
  1. Intent intent = new Intent(); 
  2. intent.setAction("android.intent.action.View"); 
  3. startActivity(intent);

二、IntentFilter匹配规则详解

1、Action的匹配规则

 
 
 
 
  1.  
  2.             
  3.                 
  4.                 
  5.                 //必须添加category android:name="android.intent.category.DEFAULT"否则报错
  6.                 
  7.             
  8.         
  9.         
  10.             
  11.                 
  12.                 
  13.             
  14.         
  15. btn_skip_b.setOnClickListener {
  16.             //A中点击按钮启动B
  17.             var intent = Intent()
  18.             intent.action = "com.ysl.test"
  19.             startActivity(intent)
  20.         }

常见action如下(Intent类中的常量)

2、category的匹配规则

category是一个字符串,系统预定义了一些category,同时我们也可以在应用中定义自己的category;

category的匹配规则是:

注意:

 
 
 
 
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. Intent intent = new Intent();
  14. intent.addCategory("com.yu.hu.category1");
  15. intent.addCategory("com.yu.hu.category2");
  16. intent.setAction("com.yu.hu.what");
  17. startActivity(intent);

3、data的匹配规则

data的匹配规则:Intent中必须含有data数据,并且data数据能够完全匹配过滤规则中的某一个data;

data的语法格式

 
 
 
 
  1.     android:host="string"
  2.     android:port="string"
  3.     android:path="string"
  4.     android:pathPattern="string"
  5.     android:pathPrefix="string"
  6.     android:mimeType="string" />

data由两部分组成: mimeType和 URI,URI通过如下格式,包括scheme、host、port、path、pathPrefix和pathPattern;

 
 
 
 
  1. ://:/[||]

具体的参数解释:

data的注意事项

 
 
 
 
  1.    
  2.    
  3.    
  4.      android:host="www.baidu.com"
  5.      android:pathPrefix="/imgs"
  6.      android:port="8080"
  7.      android:scheme="https" />
  8.  
  9.   Intent intent = new Intent();
  10.   intent.setData(Uri.parse("https://www.baidu.com:8080/imgs/img1.png"));
  11.   startActivity(intent);

三、IntentFilter总结

1、IntentFilter匹配优先级

查看Intent的过滤器(intent-filter),按照以下优先关系查找:action->data->category;

2、隐式intent;

每一个通过 startActivity() 方法发出的隐式 Intent 都至少有一个 category,就是 android.intent.category.DEFAULT,所以只要是想接收一个隐式 Intent 的 Activity 都应该包括 android.intent.category.DEFAULTcategory,不然将导致 Intent 匹配失败

说一个activity组件要想被其他组件通过隐式intent调用, 则其在AndroiddManifest.xml中的声明如下:

 
 
 
 
  1.        
  2.            
  3.            
  4.   

总结

快年底了,大家要努力学习,可以找个好工作;

本文转载自微信公众号「Android开发编程」


新闻名称:详细分析IntentFilter的匹配规则
分享链接:http://cdbrznjsb.com/article/dhdpjhd.html

其他资讯

让你的专属顾问为你服务