編輯:Android 開發實例
一、什麼是 Intent?
Intent 的中文意思就是「目的」。在 Android 中同樣是這個含義——它代表我們要去哪裡。當你想從這個 Activity 前往另一個 Activity,就得靠 Intent。
程式碼範例一:
1: //定義一個Intent
2: Intent intent = new Intent(IntentDemo.this, AnotherActivity2.class);
3: //啟動Activity
4: startActivity(intent);
上述程式碼的作用,是從 IntentDemo 這個 Activity 切換到 AnotherActivity2。這是 Intent 的一種建構方式,明確指定兩個 Activity。為什麼要指定兩個活動呢?因為 Android 有一個活動堆疊,這樣的建構方式才能正確地將前一個活動壓入堆疊,才能在按下返回鍵時讓活動正確出堆疊。
注意:所有 Activity 都必須先在 AndroidManifest.xml 中設定宣告。以下為本文用到的程式設定檔:
1: <?xml version="1.0" encoding="utf-8"?>
2: <manifest xmlns:android="https://schemas.android.com/apk/res/android"
3: package="com.halzhang.android.intent" android:versionCode="1"
4: android:versionName="1.0">
5: <application android:icon="@drawable/icon" android:label="@string/app_name">
6: <activity android:name=".IntentDemo" android:label="@string/app_name">
7: <intent-filter>
8: <action android:name="android.intent.action.MAIN" />
9: <category android:name="android.intent.category.LAUNCHER" />
10: </intent-filter>
11: </activity>
12: <activity android:name=".AnotherActivity" android:label="another">
13: <intent-filter>
14: <action android:name="android.intent.action.EDIT" />
15: <!-- category一定要配置,否則報錯:找不到Activity -->
16: <category android:name="android.intent.category.DEFAULT" />
17: </intent-filter>
18: </activity>
19:
20: <activity android:name=".AnotherActivity2" android:label="another2">
21: <intent-filter>
22: <action android:name="android.intent.action.EDIT" />
23: <category android:name="android.intent.category.DEFAULT" />
24: </intent-filter>
25: </activity>
26: </application>
27: <uses-sdk android:minSdkVersion="3" />
28: <!--
29: 上面配置的兩個activity具有相同的action類型,都為"android.intent.action.EDIT"
30: 當Intent的action屬性為Intent.ACTION_EDIT時,系統不知道轉向哪個Activity時,
31: 就會彈出一個Dialog列出所有action為"android.intent.action.EDIT"的
32: Activity供用戶選擇
33: -->
34: </manifest>
二、Intent 的建構函式
公共建構函式:
1、Intent():空建構函式。
2、Intent(Intent o):複製建構函式。
3、Intent(String action):指定 action 類型的建構函式。
4、Intent(String action, Uri uri):指定 Action 類型與 Uri 的建構函式,URI 主要用於程式間透過 ContentProvider 共享資料。
5、Intent(Context packageContext, Class<?> cls):傳入元件的建構函式,也就是上文提到的寫法。
6、Intent(String action, Uri uri, Context packageContext, Class<?> cls):前兩者的結合體。
Intent 共有六種建構函式,其中 3、4、5 最為常用,但並不代表其他幾種沒有用。Intent(String action, Uri uri) 中的 action 對應 AndroidManifest.xml 中 action 節點的 name 屬性值;Intent 類別中定義了許多 Action 與 Category 常量。
程式碼範例二:
1: Intent intent = new Intent(Intent.ACTION_EDIT, null);
2: startActivity(intent);
範例二使用了第四種建構函式,只是把 uri 參數設為 null。執行此程式碼時,系統會在主設定檔 AndroidManifest.xml 中尋找 <action android:name="android.intent.action.EDIT" /> 對應的 Activity;若有多個 Activity 都符合 <action android:name="android.intent.action.EDIT" />,此時就會彈出一個對話框供使用者選擇,如下圖。若改用範例一那種明確指定元件的寫法,就不會出現這種情況。
三、利用 Intent 在 Activity 之間傳遞資料
在 Main 中執行以下程式碼:
1: Bundle bundle = new Bundle();
2: bundle.putStringArray("NAMEARR", nameArr);3: Intent intent = new Intent(Main.this, CountList.class);
4: intent.putExtras(bundle);
5: startActivity(intent);
在 CountList 中,程式碼如下:
1: Bundle bundle = this.getIntent().getExtras();
2: String[] arrName = bundle.getStringArray("NAMEARR");以上程式碼就實現了 Activity 之間的資料傳遞。
轉自:https://www.cnblogs.com/halzhang/archive/2010/05/28/1746592.html
Android 彈出視窗的兩種做法:PopupWindow 與 Activity 偽窗口
目前見過的彈出視窗實作主要有兩種:最常見的 PopupWindow,以及用 Activity 假裝成彈窗的寫法。後者看起來相當有創意——沒想到 Activity 也能拿來
Android JSON 解析實戰:用 JSONObject 取出你真正需要的欄位
JSON 是 JavaScript Object Notation 的縮寫,作為一種輕量、語言中立的資料交換格式,它比 XML 更精簡、更容易閱讀,因此在 Android
Android 端如何解析 JSON?從天氣 API 完整實作
JSON 的全名是 JavaScript Object Notation。雖然源自 JavaScript 的物件字面語法,它早已成為獨立、跨語言的資料交換格式,憑藉著簡潔
Android 筆記:configChanges 與 onConfigurationChanged 到底解決了什麼問題
做 Android 開發,很難避開在項目裡集成各類廣告 SDK。把一堆 SDK 塞進來之後你會發現,凡是需要在 AndroidManifest.xml 裡聲明 Activ