码迷,mamicode.com
首页 > 移动开发 > 详细

Android打开飞行模式和拔出耳机时自动退出FM Radio应用

时间:2015-01-16 22:30:08      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:fmradio   mediatek   收音机   耳机   天线   

FM收音机需要插入耳机作为天线,拔出耳机时要自动退出

1.首先添加提示字符串

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

<!--TChip ZJ Add Below-->
<string name="toast_plugin_headphone_before_FM">The FM will close because you have been plug out the headphone</string>
<string name="toast_fm_enter_airplane_mode">The FM will close because you have been entered the airplane mode</string>

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);


Android打开飞行模式和拔出耳机时自动退出FM Radio应用

标签:fmradio   mediatek   收音机   耳机   天线   

原文地址:http://blog.csdn.net/zhoumushui/article/details/42783507

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!