Intent 详细讲解

1,显示匹配(Explicit):

1
2
3
4
5
6
7
8
9
10
11
12
13
public  TestB extents Activity
{
  .........
};
  public  class  Test extends  Activity
{
      ......
      public  void  switchActivity()
      {
             Intent i = new  Intent(Test. this , TestB. class );
             this .startActivity(i);
      }
}

2,隐式匹配(Implicit):
   
  
隐式匹配,首先要匹配Intent的几项值:Action, Category, Data/Type,Component。如果填写了Componet就是上例中的Test.class)这就形成了显示匹配。所以此部分只讲前几种匹配。匹配规则为最大匹配规则,

1,如果你填写了Action,如果有一个程序的Manifest.xml中的某一个Activity的IntentFilter段中定义了包含了相同的Action那么这个Intent就与这个目标Action匹配,如果这个Filter段中没有定义Type,Category,那么这个Activity就匹配了。但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。
Action的值在Android中有很多预定义,如果你想直接转到你自己定义的Intent接收者,你可以在接收者的IntentFilter中加入一个自定义的Action值(同时要设定Category值为"android.intent.category.DEFAULT"),在你的Intent中设定该值为Intent的Action,就直接能跳转到你自己的Intent接收者中。因为这个Action在系统中是唯一的。


2,data/type,你可以用Uri来做为data,比如Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);手机的Intent分发过程中,会根据http://www.google.com 的scheme判断出数据类型type
手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能处理http:的type,

3,至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的IntentFilter中包含android.category.DEFAULT,这样所有不设置Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。

4,extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

用 Intent 激活电话拨号程序

  这里使用一个Intent打开电话拨号程序,Intent的行为是ACTION_DIAL,同时在Intent中传递被呼叫人的电话号码。

  在用户界面中加入一个Button按钮,编辑res/layout/main.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
>
<Button
android:id = "@+id/button_id"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text= "@string/button"
/>
</LinearLayout><span style= "font-family: verdana, 'courier new'; font-size: 14pt;" ><span style= "white-space: normal;" >
</span></span>

我们把Button的id设置为button_id, 同时将Button显示在界面上的文字设置为res/string.xml/下的Button,打开res/string.xml,把button的内容设置为“拨号”:

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<resources>
<string name= "button" >拨号</string>
<string name= "app_name" >TinyDialer</string>
</resources><span style= "font-family: verdana, 'courier new'; font-size: 14pt;" ><span style= "white-space: normal;" >
</span></span>

创建TinyDialer的Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public  class  TinyDialer extends  Activity {
  /** Called when the Activity is first created. */
  @Override
  public  void  onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     setContentView(R.layout.main);
 
     final  Button button = (Button) findViewById(R.id.button_id);
     button.setOnClickListener( new  Button.OnClickListener() {
       @Override
       public  void  onClick(View b) {
          Intent i = new  Intent(Intent.ACTION_DIAL,
                       Uri.parse( "tel://13800138000" ));
          startActivity(i);
       }
     });
  }
}


用Intent调用系统中经常被用到的组件

1,web浏览器

Uri uri= Uri.parse("http://kuikui.javaeye.com");

returnIt = new Intent(Intent.ACTION_VIEW, uri);

2,地图

Uri mapUri = Uri.parse("geo:38.899533,-77.036476");

returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

3,调拨打电话界面

Uri telUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_DIAL, telUri);

4,直接拨打电话

Uri callUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_CALL, callUri);

5,卸载

Uri uninstallUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

6,安装

Uri installUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

7,播放

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");

returnIt = new Intent(Intent.ACTION_VIEW, playUri);

8,掉用发邮件

Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");

returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);

9,发邮件

returnIt = new Intent(Intent.ACTION_SEND);

String[] tos = { "shenrenkui@gmail.com" };

String[] ccs = { "shenrenkui@gmail.com" };

returnIt.putExtra(Intent.EXTRA_EMAIL, tos);

returnIt.putExtra(Intent.EXTRA_CC, ccs);

returnIt.putExtra(Intent.EXTRA_TEXT, "body");

returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");

returnIt.setType("message/rfc882");

Intent.createChooser(returnIt, "Choose Email Client");

10,发短信

Uri smsUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_VIEW, smsUri);

returnIt.putExtra("sms_body", "shenrenkui");

returnIt.setType("vnd.android-dir/mms-sms");

11,直接发邮件

Uri smsToUri = Uri.parse("smsto://100861");

returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);

returnIt.putExtra("sms_body", "shenrenkui");

12,发彩信

Uri mmsUri = Uri.parse("content://media/external/images/media/23");

returnIt = new Intent(Intent.ACTION_SEND);

returnIt.putExtra("sms_body", "shenrenkui");

returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);

returnIt.setType("image/png");

在 Android 应用开发中,`<intent-filter>` 是一个非常关键的组件,它定义了组件(如 `Activity`、`Service` 或 `BroadcastReceiver`)能够响应哪些类型的 `Intent` 请求。通过 `<intent-filter>`,系统可以根据用户的意图选择合适的组件来处理请求。 ### IntentFilter 的作用 `<intent-filter>` 用于声明组件可以接收的 `Intent` 类型。其主要作用包括: - **匹配启动请求**:例如应用主界面的启动(桌面图标点击)。 - **支持隐式调用**:允许其他应用通过特定的 `Intent` 启动当前应用的组件。 - **过滤数据类型**:限制组件仅能处理特定格式的数据(如特定的 URI 或 MIME 类型)。 每个 `<intent-filter>` 可以包含以下元素: - `<action>`:指定组件能处理的动作(如 `android.intent.action.VIEW`)。 - `<category>`:指定动作的附加类别信息(如 `android.intent.category.BROWSABLE`)。 - `<data>`:指定组件能处理的数据类型和格式(如 URI 结构或 MIME 类型)。 ### 使用方法详解 #### 基本结构 ```xml <intent-filter> <action android:name="android.intent.action.ACTION_NAME" /> <category android:name="android.intent.category.CATEGORY_NAME" /> <data android:scheme="http" android:host="www.example.com" /> </intent-filter> ``` #### 示例解析 ##### 案例一:设置应用主入口 ```xml <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 该配置表示 `MainActivity` 是应用的入口点,当用户点击桌面图标时会启动这个 Activity[^2]。 ##### 案例二:处理网页链接 ```xml <activity android:name=".WebBrowserActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" android:host="www.example.com" /> </intent-filter> </activity> ``` 此配置表示 `WebBrowserActivity` 可以处理指向 `http://www.example.com` 的网页链接,并且可以在浏览器上下文中被调用[^2]。 ##### 案例三:接收文件分享 ```xml <activity android:name=".ShareFileActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> </activity> ``` 此配置表示 `ShareFileActivity` 可以接收来自其他应用的图片分享请求,并处理所有图片类型的 `Intent` 数据[^2]。 ### 匹配规则 Android 系统根据以下逻辑进行匹配: 1. **Action 匹配**:`Intent` 中的 action 必须与 `<intent-filter>` 中的一个 `<action>` 完全匹配。 2. **Category 匹配**:`Intent` 中的所有 category 都必须出现在 `<intent-filter>` 中。 3. **Data 匹配**:`Intent` 中的 data 必须满足 `<intent-filter>` 中定义的数据规则(如 scheme、host、mimeType 等)。 如果上述条件都满足,则认为该组件是可接受该 Intent 的候选组件。 ### 注意事项 - 如果没有为组件定义 `<intent-filter>`,则只能通过显式 Intent 调用该组件。 - 多个 `<intent-filter>` 可以同时存在于一个组件中,表示该组件可以响应多种类型的 Intent。 - 对于隐式 Intent,系统会根据 `<intent-filter>` 决定哪个组件最适合处理该请求。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值