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

Android 編程實現飛行模式切換與調用系統應用

編輯:Android 開發實例

在做一個定時切換情景模式的小程式時,需要透過編碼來控制飛行模式。網上相關的資料不多,反覆摸索後終於理清了思路,這裡就來分享具體實現。

切換飛行模式

protected void offLine(boolean setAirPlane) {
    Settings.System.putInt(getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("TestCode", "ellic");
    sendBroadcast(intent);
}

而判斷手機當前是否處於飛行模式,則可以用這行代碼:

boolean AirPlaneModeOn = Settings.System.getInt(
        mContext.getContentResolver(),
        Settings.System.AIRPLANE_MODE_ON, 0) == 1 ? true : false;

用 Intent 調用系統應用

說到調用系統自帶的應用,最便捷的方式莫過於透過 Intent 直接激活。Intent 真是個不可多得的好東西,值得反覆琢磨。下面用幾個例子說明。

1. 調用系統郵件程序:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hello World!");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "It is body-Hello World!");
startActivity(Intent.createChooser(emailIntent, "Sending mail..."));

2. 調用系統短信程序:

Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);

3. 調用系統鬧鐘程序:這裡要特別提醒,不同 SDK、不同廠商的設備,鬧鐘的類名可能都不一樣。比如在 Motorola Defy 上,鬧鐘的 classname 是 com.motorola.blur.alarmclock 而非 com.android.alarmclock.AlarmClock,因此需要用 setClassName(String packageName, String className) 方法做相應調整:

Intent intent = new Intent();
intent.setClassName("com.android.alarmclock", "com.android.alarmclock.AlarmClock");
startActivity(intent);

透過這些示例不難看出,Intent 簡直是打通 Android 各應用之間的「萬能鑰匙」。

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