当前位置:操作系统 > 安卓/Android >>

Android FM播放时拔出耳机或者进入飞行模式,FM APP出现相应提示框,然后自动停止 m

正文
  
 
部分客户要求如下:
1.FM播放时拔出耳机后,FM APP出现“拔出耳机,Fm停止"的提示框,然后自动close ;
2.FM播放时进入飞行模式后,FM APP出现“进入飞行模式,Fm停止"的提示框,然后自动close.
 
修改FMRadioService.java
1.添加如下import,以便能显示toast
////AAAAA add start
import android.widget.Toast;
////AAAAA add end
public class FMRadioService extends Service implements FMRecorder.OnRecorderStateChangedListener {
 
2.在下面函数onBind()前添加 
//AAAAA 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);
    };
//AAAAA add end
@Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
 
3.修改onReceive 方法,这样子就可以显示toast以及关闭Fm app了
在  switchAntennaAsync(mValueHeadSetPlug);
之后添加:
 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);
 
4.修改onReceive 方法,这样子就可以在进入飞行模式时显示toast以及在关闭Fm app了
        public void onReceive(Context context, Intent intent)
else {
                LogUtils.w(TAG, "Error: undefined action.");
  }
之前添加:
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(sHandler.obtainMessage(MSG_EXIT_FM, FMRadioService.this),(long)1000); 
                  mFmServiceHandler.removeCallbacksAndMessages(null); 
                  stopFMFocusLoss(AudioManager.AUDIOFOCUS_LOSS);
                   }
             }  
}
 
4.修改    public void onCreate() 
在        filter.addAction(Intent.ACTION_SCREEN_OFF);
之后添加        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
 
5.在alps\mediatek\packages\apps\FMRadio\res\values\strings.xml中添加
<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>
 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,