Android之Intent和Activity,androidintent
Intent可以說是Android的靈魂,程式跳轉和傳遞資料的時候基本上就是靠Intent了。Intent在Android應用中是相當重要的,理解Intent對應用編程很有協助。在Android的官方API文檔裡邊對Intent是這樣定義的:An Intent is an abstract description of an operation to be performed。一個Intent就是一次對將要執行的操作的抽象描述(真實夠抽象的)。具體有一下3種形式:
1、通過startActivity方法來啟動一個新的Activity。
2、通過Broadcast機制可以將一個Intent發送給任何對這個Intent感興趣的BroadcastReceiver。
3、通過startService(Intent)或bindService(Intent, ServiceConnection, int)來和背景Service互動。
下面來看一下如何通過startActivity方法啟動一個新的Activity。
Intent最常用的用途就是串連一個應用當中的各個Activity,如果我們把Activity比作積木的話,那麼Intent就好像是膠水,把不同的積木粘起來,構成我們搭建的房子。在程式當中如果要啟動一個Activity的話,通常我們會調用startActivity方法,並把Intent作為參數傳進去,如下所示:
startActivity(intent);
這個Intent或者指定了一個Activity,或者裡邊只包含了選定Activity的資訊,但是具體啟動哪個Activity是由系統去決定的,Android系統負責挑選一個最滿足匹配挑選條件的Activity。
執行個體:IntentDemo
運行效果:
代碼清單:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rainsong.intentdemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="OtherActivity" android:label="OtherActivity"> </activity> </application></manifest>
布局檔案:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳轉到另一個Activity" /></LinearLayout>
布局檔案:other.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" ><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="OtherActivity" /></LinearLayout>
Java原始碼檔案:MainActivity.java
package com.rainsong.intentdemo;import android.app.Activity;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{ OnClickListener listener1 = null; Button button1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listener1 = new OnClickListener() { public void onClick(View v) { Intent intent1= new Intent(MainActivity.this, OtherActivity.class); startActivity(intent1); } }; button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(listener1); }}
Java原始碼檔案:OtherActivity.java
package com.rainsong.intentdemo;import android.app.Activity;import android.os.Bundle;public class OtherActivity extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); }}