隱式intent,intent

來源:互聯網
上載者:User

隱式intent,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     <application11         android:allowBackup="true"12         android:icon="@drawable/ic_launcher"13         android:label="@string/app_name"14         android:theme="@style/AppTheme" >15         <activity16             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     @Override17     protected void onCreate(Bundle savedInstanceState) {18         // TODO Auto-generated method stub19         super.onCreate(savedInstanceState);20         setContentView(R.layout.activity_main);21         btn_one=(Button) findViewById(R.id.button1);22         btn_one.setOnClickListener(this);23     }24     @Override25     public void onClick(View v) {26         // TODO Auto-generated method stub27         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     <application11         android:allowBackup="true"12         android:icon="@drawable/ic_launcher"13         android:label="@string/app_name"14         android:theme="@style/AppTheme" >15         <activity16             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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.