zoukankan      html  css  js  c++  java
  • Android的Intent和IntentFilter应用说明一例

     很多人对文档中的Intent和IntentFilter不理解是什么意思,我这里举例解释下。 Intent字面意思就是目标,目的。通俗一点,需要达成某些目标,则需要提供一些动作,这些目标的分类,以及达成这些目标所需要的一些数据等等。Android中的Intent通过Action,Category和data等属性进行了相应的描述,我们想做某些事情(达成某些目标),就需要填写这些参数的部分或全部,这样Android才会帮助我们自动的去进行某些操作。 IntentFilter是配合Intent而生的,你有目标行动或者结果,那么那些行动和结果就会有他完成的特定要求,这些要求就是IntentFilter,可以理解为Intent和IntentFilter是相对应的。

    <activity android:name=".TestService" android:label="@string/app_name">
                
    <intent-filter>
                    
    <action android:name="android.intent.action.MAIN" />
                    
    <category android:name="android.intent.category.LAUNCHER" />
                    
    <action android:name="android.intent.action.STORE_REQUEST"></action>
                    
    <category android:name="android.intent.category.ALTERNATIVE" />
                    
    <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
                
    </intent-filter>

                
    <intent-filter>
                    
    <action android:name="android.intent.action.TIGERTIAN"></action>
                    
    <category android:name="android.intent.category.DEFAULT"></category>
                    
    <data android:scheme="x-id"></data>
                
    </intent-filter>
                
                
    <intent-filter>
                    
    <action android:name="android.intent.action.EDIT"></action>
                    
    <category android:name="android.intent.category.DEFAULT"></category>
                
    <category android:name="android.intent.category.BROWSABLE"></category>
    </intent-filter>
            
    </activity>

    上面的Activity有三个Filter,第一个是给Android系统用的,表示这个Activity可以显示在桌面上(Launcher中)。同时Alternative表明,这个Activity可以 变成OptionMenu,供其他Activity直接调用。 后面两个Filter就是我自定义的了,第二个Filter可以在其他Activity中用如下方法直接调用:

          Uri uri = Uri.parse("x-id://www.google.com/getDetails?id=123");
                    Intent in 
    = new Intent();
                    in.setAction(
    "android.intent.action.TIGERTIAN");
                    in.addCategory(Intent.CATEGORY_DEFAULT);
                    in.setData(uri);
                    
    //in.setClassName("com.tigertian.service", "com.tigertian.service.TestService");
                    TestActivity.this.startActivity(in);

    在Filter配置中CATEGORY_DEFAULT是不可缺少的,想调用这个Service,可以不指定Class,但其他条件必须匹配(CATEGORY_DEFAULT可以不设置,Android默认会自动加上),通过Action,category和data就可以调用相应的Activity了,这是Android帮你做的,当然如果系统中存在多个匹配这些条件的Activity或者Service,Android根据优先级进行调用。 第三个调用方式如下:

    Uri uri = Uri.parse("x-id://www.google.com/getDetails?id=123");
                    Intent in 
    = new Intent();
                    in.setAction(Intent.ACTION_EDIT);
                    in.addCategory(Intent.CATEGORY_BROWSABLE);
                    
    //in.setData(uri);
                    
    //in.setComponent(new ComponentName("com.tigertian.service", "com.tigertian.service.TestService"));
                    TestActivity.this.startActivity(in);

    可以不指定CATEGORY_DEFAULT,Android自动帮你添加。就是这么简单。  

  • 相关阅读:
    VS中添加搜索路径和链接库的方法
    hive多分隔符支持
    shell 遍历目录下的所有文件
    使用ansible控制Hadoop服务的启动和停止【转】
    Shell中的括号有其特殊的用法
    shell中括号[]的特殊用法 linux if多条件判断
    Linux中rz和sz命令用法详解
    vim 去掉自动注释和自动回车
    ping判断局域网ip使用情况
    shell判断有效日期
  • 原文地址:https://www.cnblogs.com/navy-wang/p/3181791.html
Copyright © 2011-2022 走看看