Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發速查:以 Intent 開啟系統內建程式

編輯:關於 Android 程式設計

在開發過程中,時常需要讓應用程式跳轉到系統內建的各種設定或功能頁面。Android 透過 Intent 提供了統一的方式來實現這些跳轉,以下整理幾個常見的情境。

1. 開啟設定主介面

Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivityForResult(intent, 0);

2. 開啟網路與應用管理設定

其他設定頁面同理,替換對應的 Action 即可:

Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); // WIFI 設定
startActivity(intent);

Intent intent2 = new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS); // 管理應用程式
context.startActivity(intent2);

或者採用以下方式(3.0 以前的版本可使用):

Intent intent = new Intent("/");
ComponentName cm = new ComponentName("com.android.settings",
        "com.android.settings.WirelessSettings");
intent.setComponent(cm);
intent.setAction("android.intent.action.VIEW");
startActivityForResult(intent, 0);

3. 開啟壁紙設定

Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(intent);

4. 開啟撥號介面

Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);

5. 開啟聯絡人

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android.cursor.dir/contact");
startActivity(intent);

6. 開啟通話記錄

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android.cursor.dir/calls");
startActivity(intent);

7. 開啟簡訊列表

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);

以上都是透過 Intent 搭配系統提供的 Action 或 MIME Type 來完成頁面跳轉。掌握這套模式後,面對各種系統功能入口都能快速對應上手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved