zoukankan      html  css  js  c++  java
  • 隐式intent

    隐式intent

    一、隐式意图介绍

    显式意图我们前面已经提到,形如:

    Intent intent = new Intent();

    intent.setClass(this,Other.class); //此句表示显式意图,因为明确设置激活对象为Other类

    startActivity(intent);

    顾名思义,隐式意图就是在不明确设置激活对象的前提下寻找最匹配的组件,举个例子,比如有5个人:

    (1)A:170cm

    (2)B:160cm

    (3)C:180cm

    (4)D:190cm

    (5)E:200cm

    如果是显示意图的话,如果我们要指明选择A的话会说:”我选择A.“,但是如果是隐式意图,则会说:”我要选择170cm的人“,虽然没有指明要选A,但会寻找条件最匹配的人。

    在intent过滤器中类似于上面例子中的”身高“条件的匹配条件有:

    (1)action

    (2)category

    (3)data:scheme、host、path、type

    当在程序中设置了这些激活组件的条件,程序就会去寻找最匹配的组件,但是注意:只要有一点不匹配,则就是不匹配;

    比如:

    Intent intent = new Intent();

    intent.setAction("a"); //此句只是指定了Action

    startActivity(intent); //寻找最匹配的组件激活,内部会调用intent.addCategory("Android.intent.category.DEFAULT"); 

    参考:http://blog.csdn.net/xiazdong/article/details/7764865

    二、代码实例

    com.example.implicit2a_Intent.MainActivity

    package com.example.implicit2a_Intent;
    
    
    
    
    import com.example.implicit2_intent.R;
    
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements OnClickListener{
        private Button btn_one;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn_one=(Button) findViewById(R.id.button1);
            btn_one.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent();
            /**
             * 找所有action符合为"com.fry.activity01"的页面
             * 也找所有Category为android.intent.category.DEFAULT的页面
             * String android.content.Intent.CATEGORY_DEFAULT = 
             * "android.intent.category.DEFAULT"
             */
            intent.setAction("com.fry.activity01");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }
    
        
    }

    /implicit2_Intent/AndroidManifest.xml

     1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     2     package="com.example.implicit2_intent"
     3     android:versionCode="1"
     4     android:versionName="1.0" >
     5 
     6     <uses-sdk
     7         android:minSdkVersion="8"
     8         android:targetSdkVersion="19" />
     9 
    10     <application
    11         android:allowBackup="true"
    12         android:icon="@drawable/ic_launcher"
    13         android:label="@string/app_name"
    14         android:theme="@style/AppTheme" >
    15         <activity
    16             android:name="com.example.implicit2a_Intent.MainActivity"
    17             android:label="@string/app_name" >
    18             <intent-filter>
    19                 <action android:name="android.intent.action.MAIN" />
    20 
    21                 <category android:name="android.intent.category.LAUNCHER" />
    22             </intent-filter>
    23         </activity>
    24         <activity android:name="com.example.implicit2a_Intent.Activity03">
    25             <intent-filter >
    26                 <action android:name="com.fry.activity01"></action>
    27                 <category android:name="android.intent.category.DEFAULT"/>
    28             </intent-filter>
    29         </activity>
    30     </application>
    31 
    32 </manifest>

    另一个应用

    com.example.implicit1Intent.MainActivity

     1 package com.example.implicit1Intent;
     2 
     3 
     4 import com.example.implicitintent.R;
     5 
     6 import android.app.Activity;
     7 import android.content.DialogInterface;
     8 import android.content.Intent;
     9 import android.os.Bundle;
    10 import android.view.View;
    11 import android.view.View.OnClickListener;
    12 import android.widget.Button;
    13 
    14 public class MainActivity extends Activity implements OnClickListener{
    15     private Button btn_one;
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         // TODO Auto-generated method stub
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.activity_main);
    21         btn_one=(Button) findViewById(R.id.button1);
    22         btn_one.setOnClickListener(this);
    23     }
    24     @Override
    25     public void onClick(View v) {
    26         // TODO Auto-generated method stub
    27         Intent intent=new Intent();
    28         /**
    29          * 找所有action符合为"com.fry.activity01"的页面
    30          * 也找所有Category为android.intent.category.DEFAULT的页面
    31          * String android.content.Intent.CATEGORY_DEFAULT = 
    32          * "android.intent.category.DEFAULT"
    33          */
    34         intent.setAction("com.fry.activity01");
    35         intent.addCategory(Intent.CATEGORY_DEFAULT);
    36         startActivity(intent);
    37     }
    38 
    39     
    40 }

    /implicit_Intent/AndroidManifest.xml

     1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     2     package="com.example.implicitintent"
     3     android:versionCode="1"
     4     android:versionName="1.0" >
     5 
     6     <uses-sdk
     7         android:minSdkVersion="8"
     8         android:targetSdkVersion="19" />
     9 
    10     <application
    11         android:allowBackup="true"
    12         android:icon="@drawable/ic_launcher"
    13         android:label="@string/app_name"
    14         android:theme="@style/AppTheme" >
    15         <activity
    16             android:name="com.example.implicit1Intent.MainActivity"
    17             android:label="@string/app_name" >
    18             <intent-filter>
    19                 <action android:name="android.intent.action.MAIN" />
    20 
    21                 <category android:name="android.intent.category.LAUNCHER" />
    22             </intent-filter>
    23         </activity>
    24         <activity android:name="com.example.implicit1Intent.Activity01">
    25             <intent-filter >
    26                 <action android:name="com.fry.activity01"></action>
    27                 <category android:name="android.intent.category.DEFAULT"/>
    28             </intent-filter>
    29         </activity>
    30         <activity android:name="com.example.implicit1Intent.Activity02">
    31             <intent-filter >
    32                 <action android:name="com.fry.activity01"></action>
    33                 <category android:name="android.intent.category.DEFAULT"/>
    34             </intent-filter>
    35         </activity>
    36     </application>
    37 
    38 </manifest>
  • 相关阅读:
    python的argparse模块
    Robotframework之SSHLibrary库
    Python 中的 getopt 模块
    Python list 列表
    Samba windows 10 share: mount error(112): Host is down
    安装两个版本的python安装包,后安装的python程序打开时闪退
    NetScaler VPX configration
    drupal smtp could not connect to smtp
    drupal7 判断用户是否具有某个权限
    微信支付报错:time_expire时间过短,刷卡至少1分钟,其他5分钟]
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/7253915.html
Copyright © 2011-2022 走看看