正文
部分客戶要求如下:
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>