android FM播放时拔出耳机后,FM APP出现"拔出耳机,Fm停止"的提示框,然后自动close
客户要求FM播放时拔出耳机后,FM APP出现“拔出耳机,Fm停止"的提示框,然后自动close
修改FMRadioService.java
1.添加如下import,以便能显示toast
////AAAAA add start
import android.widget.Toast;
////AAAAA add end
////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) {
FMRadioLogUtils.d(TAG, "sHandler11 = " + msg.what);
switch (msg.what) {
case MSG_EXIT_FM:
android.os.Process.killProcess(android.os.Process.myPid());
break;
}
}
};
//AAAAA add start
public static final int MSG_EXIT_FM = 1234;
private Handler sHandler11 = new Handler() {
@Override
public void handleMessage(Message msg) {
FMRadioLogUtils.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;
}
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(sHandler.obtainMessage(MSG_EXIT_FM, FMRadioService.this),(long)
在 switchAntennaAsync(mValueHeadSetPlug);
之后添加:
if(mValueHeadSetPlug == 1 && mIsPowerUp )
{
showToast(getString(R.string.toast_plugin_headphone_before_FM));
sHandler11.sendMessageDelayed(sHandler.obtainMessage(MSG_EXIT_FM, FMRadioService.this),(long)
1000);
mFmServiceHandler.removeCallbacksAndMessages(null);
stopFMFocusLoss(AudioManager.AUDIOFOCUS_LOSS);
}
else
sHandler11.removeMessages(MSG_EXIT_FM);
stopFMFocusLoss(AudioManager.AUDIOFOCUS_LOSS);
}
else
sHandler11.removeMessages(MSG_EXIT_FM);
4.在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
<string name="toast_plugin_headphone_before_FM">The FM will close because you have been plug out
the headphone</string>