編輯:關於 Android 程式設計
啟動 Activity 是 Android 四大組件裡最基礎的操作,而「怎麼啟動」有兩條路:顯式意圖和隱式意圖。理解兩者的差別,才能寫出既清晰又健壮的頁面跳轉代碼。
activity_main.xml 中放四個按鈕,分別演示四種啟動方式:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.twoactivity.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第一個界面" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="跳轉到第二個界面" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="激活系統的一個應用" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="隱式意圖跳轉到第二個界面" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click4"
android:text="激活短信的界面" />
</LinearLayout>
activity_two.xml 略簡單,一個提示文本、一張圖片、一個返回按鈕即可。
package com.example.twoactivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** 方式一:顯式意圖,直接指定目標類 */
public void click(View view) {
// 等價寫法:
// Intent intent = new Intent();
// intent.setClassName(this, "com.example.twoactivity.OtherScreenActivity");
// startActivity(intent);
Intent intent = new Intent(this, OtherScreenActivity.class);
startActivity(intent);
}
/** 方式二:顯式意圖,激活系統裡的另一個應用 */
public void click2(View view) {
Intent intent = new Intent();
intent.setClassName("com.android.gallery",
"com.android.camera.GalleryPicker");
startActivity(intent);
}
/** 方式三:隱式意圖,靠 action + category + data 匹配 */
public void click3(View view) {
Intent intent = new Intent();
intent.setAction("com.example.xxx");
intent.addCategory("android.intent.category.DEFAULT");
// 只指定數據類型
// intent.setType("vnd.android.cursor.item/haha");
// 只指定數據
// intent.setData(Uri.parse("itheima:gagaga"));
// 類型與數據都有,使用這個方法
intent.setDataAndTypeAndNormalize(
Uri.parse("itheima:gagaga"), "vnd.android.cursor.item/haha");
startActivity(intent);
}
/** 方式四:隱式意圖,激活系統短信界面 */
public void click4(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.SENDTO");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("sms:110"));
startActivity(intent);
}
}
package com.example.twoactivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
/**
* Activity 是系統的重要組件之一。
* 操作系統要想找到 Activity,就必須在清單文件中配置。
*/
public class OtherScreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
// 獲取激活它的意圖,並讀取攜帶的數據
Intent intent = getIntent();
Uri uri = intent.getData();
String result = uri.toString();
Toast.makeText(this, "數據是:" + result, 0).show();
}
}
隱式意圖能被匹配上,關鍵在 intent-filter:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.ce"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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=".ResultActivity"
android:label="@string/app_name" >
<!-- 若要用隱式意圖激活,需在此聲明匹配規則 -->
<intent-filter>
<action android:name="com.example.xxx" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="itheima"
android:host="gagaga"
android:mimeType="vnd.android.cursor.item/haha" />
</intent-filter>
</activity>
</application>
</manifest>
顯式意圖明確告訴系統「我要啟動哪個組件」,直接給出 ComponentName(包名 + 類名)。系統無需做任何匹配,開銷最小,跳轉結果確定。適用於應用內部的頁面跳轉。
隱式意圖只描述「我想做什麼」(action)、「給誰做」(data / type)、「附加條件」(category),由系統去所有已安裝應用的 intent-filter 中查找合適的目標。適用於跨應用調用,比如打開網頁、調起相冊、發送郵件。
1. setData 會覆蓋 type,反之亦然
當 scheme 和 mimeType 都需要指定時,必須用 setDataAndType() 或示例中的 setDataAndTypeAndNormalize(),否則後調用的那個會把前一個沖掉。
2. 隱式意圖一定要 try-catch
如果設備上沒有任何應用能處理你的 Intent,startActivity() 會拋 ActivityNotFoundException 直接崩潰:
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "沒有合適的應用可以處理", Toast.LENGTH_SHORT).show();
}
3. 自定義 action 的命名規範
應使用「反向域名 + 描述」的形式,如 com.example.app.action.VIEW_DETAIL,避免與其他應用的 action 衝突。
4. Android 11 的包可見性限制
API 30 之後引入 package visibility,若需查詢其他應用的組件(queryIntentActivities())或在 setClassName() 中引用外部包,必須在清單裡用 <queries> 明確聲明,否則查不到。示例中 click2() 硬編碼系統相冊類名的做法,在真實項目裡應當改用官方 MediaStore / ACTION_PICK 方式。
5. 硬編碼類名極其脆弱
com.android.gallery 這種寫法在絕大多數設備上都不存在(各家 ROM 的相冊包名各不相同)。跨應用調用務必使用標準 action,例如 Intent.ACTION_PICK 配 Images.Media.EXTERNAL_CONTENT_URI。
掌握了這兩種啟動方式,Activity 之間的跳轉基本就沒有難點了。
Android 自訂加載狀態檢視:Loading、Empty、Error 一站式處理
為什麼需要統一加載狀態 應用程式在等待資料時,常需要顯示加載中、無網路、無資料或加載失敗等狀態。若每個頁面都各自實作,容易風格不一且難以維護。把這些狀態封裝成一個自訂 V
自定义 ListView 实现拖拽 Item 项交换位置(附源码)
在上一篇中,我们通过布局泵拿到不同布局为 listitem 布局,实现了联系人的 ListView。这一章要做的是拖拽 ListView 的 Item 项,实现拖动后数据
Android 源碼:Activity 的啟動流程全解析
本文梳理一次 Activity 從 startActivity 到真正創建、啟動的完整源碼流程。 1. 從 startActivity 說起 啟動代碼很簡單: Inten
自製可自動平分空間的 Android 宮格檢視 AdvancedGridView
專案裡常會需要九宮格,而且不只九宮格——四宮格、十六宮格、4×3 宮格都可能出現。與其每次重寫,不如封裝一個能依分配到的空間自動調整每一格尺寸的元件。 設計思路 這個