Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android打開飛行模式和拔出耳機時自動退出FM Radio應用

Android打開飛行模式和拔出耳機時自動退出FM Radio應用

編輯:關於Android編程

FM收音機需要插入耳機作為天線,拔出耳機時要自動退出

1.首先添加提示字符串

mediatek/packages/apps/FMRadio/res/values/strings.xml


The FM will close because you have been plug out the headphone
The FM will close because you have been entered the airplane mode

2.修改FMRadioService

mediatek/packages/apps/FMRadio/src/com/mediatek/FMRadio/FMRadioService.java

--- a/mediatek/packages/apps/FMRadio/src/com/mediatek/FMRadio/FMRadioService.java
+++ b/mediatek/packages/apps/FMRadio/src/com/mediatek/FMRadio/FMRadioService.java
@@ -227,6 +227,34 @@ public class FMRadioService extends Service implements FMRecorder.OnRecorderStat
     // Add this lock the exitFM() while stopRecording()
     private Object mStopRecordingLock = new Object();
  
+
+
+//{@TChip ZJ Add START
+
+ public static final int MSG_EXIT_FM = 1234;
+ private Handler sHandler11 = new Handler() {
+   @Override
+   public void handleMessage(Message msg) {
+    LogUtils.d(TAG, "sHandler11 =  " + msg.what);
+    switch (msg.what) {
+     case MSG_EXIT_FM:
+      android.os.Process.killProcess(android.os.Process.myPid());
+      break;
+    }
+   }
+ };
+ private Toast mToast = null;
+ private void showToast(CharSequence text) {
+ if (null == mToast) {
+     mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
+     }
+        mToast.setText(text);
+        mToast.show();
+        LogUtils.v(TAG, "FMRadioActivity.showToast: toast = " + text);
+    };
+
+// TChip ZJ Add END@}
+
     @Override
     public IBinder onBind(Intent intent) {
         LogUtils.d(TAG, "FMRadioService.onBind: " + intent);
@@ -319,6 +347,21 @@ public class FMRadioService extends Service implements FMRecorder.OnRecorderStat
                 // switch antenna should not impact audio focus status
                 mValueHeadSetPlug = (intent.getIntExtra("state", -1) == HEADSET_PLUG_IN) ? 0 : 1;
                 switchAntennaAsync(mValueHeadSetPlug);
+//{@TChip ZJ Add START
+
+if(mValueHeadSetPlug == 1 && mIsPowerUp )
+{
+   showToast(getString(R.string.toast_plugin_headphone_before_FM));
+   sHandler11.sendMessageDelayed(sHandler11.obtainMessage(MSG_EXIT_FM, FMRadioService.this),(long)
+1000);
+mFmServiceHandler.removeCallbacksAndMessages(null);
+stopFMFocusLoss(AudioManager.AUDIOFOCUS_LOSS);
+}
+ else
+ sHandler11.removeMessages(MSG_EXIT_FM);
+
+//TChip ZJ Add END
+
                 if (SHORT_ANNTENNA_SUPPORT) {
                     boolean isSwitch = (switchAntenna(mValueHeadSetPlug) == 0) ? true : false;
                     LogUtils.d(TAG, "onReceive.switch anntenna:isWitch:" + isSwitch);
@@ -380,7 +423,27 @@ public class FMRadioService extends Service implements FMRecorder.OnRecorderStat
                 LogUtils.d(TAG, "setFMViaBTController(false) succeeded!!");
                 mUsingFMViaBTController = false;
                 enableFMAudio(true);
-            } */else {
+            } */
+
+//{@TChip ZJ Add START
+
+else if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)){
+       boolean isAirPlaneMode = intent.getBooleanExtra("state",false);
+       if(isAirPlaneMode){
+           LogUtils.d(TAG, ">>>ACTION_AIRPLANE_MODE_CHANGED");
+           if (mIsPowerUp) {
+                  showToast(getString(R.string.toast_fm_enter_airplane_mode));
+                  sHandler11.sendMessageDelayed(sHandler11.obtainMessage(MSG_EXIT_FM, FMRadioService.this),(long)1000);
+                  mFmServiceHandler.removeCallbacksAndMessages(null);
+                  stopFMFocusLoss(AudioManager.AUDIOFOCUS_LOSS);
+                   }
+             } 
+}
+
+//TChip ZJ Add END @}
+
+
+else {
                 LogUtils.w(TAG, "Error: undefined action.");
             }
             LogUtils.d(TAG, "<<< FMRadioService.onReceive");
@@ -1557,6 +1620,7 @@ public class FMRadioService extends Service implements FMRecorder.OnRecorderStat
         filter.addAction(Intent.ACTION_SHUTDOWN);
         filter.addAction(Intent.ACTION_SCREEN_ON);
         filter.addAction(Intent.ACTION_SCREEN_OFF);
+filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);// TChip ZJ Add
         filter.addAction(ACTION_SHUTDOWN_IPO);
         filter.addAction(ACTION_PRE_SHUTDOWN);


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved