Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

一定要熟記的 Android Intent 常用寫法

編輯:初階開發

Intent 的寫法幾乎是固定套路,記熟幾個模式,日後要叫出系統功能就不必查文件。以下用一個含按鈕的簡單畫面,把常見情境全部走過。

版面檔 main.xml

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Button
    android:id="@+id/about_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/about_label" />

字串資源 strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello djmaxadd22!</string>
    <string name="app_name">djmaxadd22</string>
    <string name="about_label">登陸</string>
    <string name="about_info">about "hello,world"</string>
    <string name="about_title">劉磊</string>
</resources>

字串中含有雙引號時,記得用 &quot; 轉義,否則 aapt 會直接編譯失敗。

主程式:掛上監聽器

import android.content.Intent;

public class djmaxadd22 extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View v = findViewById(R.id.about_button);
        v.setOnClickListener(this);
    }
    // ...
}

讓 Activity 直接實作 OnClickListener,是最省字元的寫法;若畫面上按鈕眾多,改用匿名內部類或分開的監聽器會更好讀。

onClick 中的各種 Intent

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.about_button:

        // 用 Google 搜尋內容
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, "searchString");
        startActivity(intent);

        // 瀏覽網頁
        Uri uri = Uri.parse("https://www.baidu.com");
        startActivity(new Intent(Intent.ACTION_VIEW, uri));

        // 撥打電話(叫出撥號介面)
        Uri uri2 = Uri.parse("tel:0514-888888");
        startActivity(new Intent(Intent.ACTION_DIAL, uri2));

        // 發送簡訊
        Intent it = new Intent(Intent.ACTION_VIEW);
        it.putExtra("sms_body", "發送內容已經寫好");
        it.setType("vnd.android-dir/mms-sms");
        startActivity(it);

        // 發送郵件
        Uri uri3 = Uri.parse("mailto:11681347@qq.com");
        startActivity(new Intent(Intent.ACTION_SENDTO, uri3));

        // 喚起相機(送出廣播)
        Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
        sendBroadcast(i);

        // 打開錄音機
        Intent it2 = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        startActivity(it2);

        // 從相簿挑選圖片
        Intent i2 = new Intent();
        i2.setType("image/*");
        i2.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(i2, 11);

        // 顯示應用程式的詳細資訊
        Uri uri4 = Uri.parse("market://details?id=<packagename>");
        startActivity(new Intent(Intent.ACTION_VIEW, uri4));

        break;
    }
}

幾個實務上的修正建議

switch 中的變數宣告。Java 不允許在同一個 case 區塊裡重複宣告同名變數(如上面的 uri 寫了三次),實作時請為每個變數取不同名稱,或乾脆把每段邏輯抽成獨立方法,可读性也會好很多。

ACTION_CAMERA_BUTTON 的用途被誤會了。它本意是「使用者按了相機硬體鍵」,用來通知有照相功能的程式,而不是「開啟相機」。要真的開相機,請用 MediaStore.ACTION_IMAGE_CAPTURE 並搭配 startActivityForResult 接收照片。

RECORD_SOUND_ACTION 的正確寫法。常數實際位於 MediaStore 而非 Media,寫成 MediaStore.Audio.Media.RECORD_SOUND_ACTION。

記得防呆。market:// 與 tel: 這類意圖在沒有對應程式的裝置上(例如平板)會拋出 ActivityNotFoundException。養成習慣包 try-catch,或用 pm.resolveActivity() 先確認,能省下不少線上的崩潰報告。

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