[DESCRIPTION]
[New Feature]收音机播放时,按下耳机hook键,收音机暂停;若再次按hook键,收音机又会播放
[SOLUTION]
一、 修改FmRadioService.java
1.在文件头部添加:
import android.content.ComponentName;
import android.media.RemoteControlClient;
import android.media.RemoteControlClient.MetadataEditor;
2.在FmRadioService类的开始处添加如下:
private static final String TAG = "FmRx/Service“;
//添加以下
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMD= "command";
public static final String CMDNEXT = "cmdnext";
private RemoteControlClient mRemoteControlClient;
private ComponentName mFMMediaButtonIntentReceiver = null;
//添加以上
二、在alps\vendor\mediatek\proprietary\packages\apps\FmRadio\src\com\mediatek\fmradio下面添加一支文件:
文件名为:FMMediaButtonIntentReceiver.java
package com.mediatek.fmradio;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.util.Log;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import java.util.List;
public class FMMediaButtonIntentReceiver extends BroadcastReceiver {
public static final String TAG = "FmRx/Receiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, " mFMMediaButtonIntentReceiver onReceive");
String Iaction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(Iaction)) {
Log.d(TAG, " mFMMediaButtonIntentReceiver onReceive ACTION_MEDIA_BUTTON");
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return; }
int keycode = event.getKeyCode();
int action = event.getAction();
long eventtime = event.getEventTime();
Log.d(TAG, "onReceive keycode="+keycode+",action="+action);
String command = null;
switch (keycode) {
case KeyEvent.KEYCODE_MEDIA_STOP:
break;
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
command = FmRadioService.CMDTOGGLEPAUSE;
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
break;
/// M: AVRCP and Android Music AP supports the FF/REWIND @{
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
break;
default:
break;
}
if (command != null) {
if ((action == KeyEvent.ACTION_DOWN) && (event.getRepeatCount() == 0)) {
sendToStartService(context, command);}
if (isOrderedBroadcast()) {
abortBroadcast();}
}} }
public void sendToStartService(Context context, String command) {
Intent i = new Intent(context, FmRadioService.class);
i.putExtra(FmRadioService.CMD, command);
context.startService(i); }
}
三、在FmRadioService类的onCreate方法中添加如下:
@Override
public void onCreate() {
……
//添加以下
Log.d(TAG, "registerMediaButtonEventReceiver.mFMMediaButtonIntentReceiver" );
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName mFMMediaButtonIntentReceiver = new ComponentName(getPackageName(),
FMMediaButtonIntentReceiver.class.getName());
am.registerMediaButtonEventReceiver(mFMMediaButtonIntentReceiver);
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(mFMMediaButtonIntentReceiver);
PendingIntent pi = PendingIntent.getBroadcast(this /*context*/,
0 /*requestCode, ignored*/, i /*intent*/, 0 /*flags*/);
mRemoteControlClient = new RemoteControlClient(pi);
mAudioManager.registerRemoteControlClient(mRemoteControlClient);
int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY
| RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_STOP;
mRemoteControlClient.setTransportControlFlags(flags);
//添加以上
四、在FmRadioService类的onStartCommand方法中添加如下:
Log.d(TAG, ">>> FmRadioService.onStartCommand intent: " + intent + " startId: " + startId);
//添加如下
String cmd = intent.getStringExtra("command");
Log.d(TAG, ">>> FmRadioService.onStartCommand command: " + cmd);
if( CMDTOGGLEPAUSE.equals(cmd) ){
Log.d(TAG, "onStartCommand:CMDTOGGLEPAUSE" );
if(!mIsPowerUp){
powerUpAsync(FmRadioUtils.computeFrequency(getFrequency()));}
else{
powerDownAsync();}
}
//添加如上
五、在FmRadioService类的powerUpFm方法中添加如下:
private boolean powerUpFm(float frequency) {
……
mIsPowerUp = true;
setMute(true);
//添加以下
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
//添加以上
……
}
六、在FmRadioService类的powerDown方法中添加如下:
private boolean powerDown() {
……
if (!mIsPowerUp) {
Log.w(TAG, "Error: device is already power down.");
return true;
}
//添加以下
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
//添加以上
mIsPowerUp = false;
…… }
七、在FmRadioService类的onDestroy方法中添加如下:
public void onDestroy() {
……
abandonAudioFocus();
exitFm();
//添加以下
Log.d(TAG, "unregisterMediaButtonEventReceiver.mFMMediaButtonIntentReceiver" );
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.unregisterMediaButtonEventReceiver(mFMMediaButtonIntentReceiver);
mAudioManager.unregisterRemoteControlClient(mRemoteControlClient);
//添加以上 ……
}
八、修改alps\vendor\mediatek\proprietary\packages\apps\FmRadio\AndroidMainfest.xml,添加以下:
<receiver android:name="com.mediatek.fmradio.FMMediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
import android.media.RemoteControlClient.MetadataEditor;
2.在FmRadioService类的开始处添加如下:
private static final String TAG = "FmRx/Service“;
//添加以下
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMD= "command";
public static final String CMDNEXT = "cmdnext";
private RemoteControlClient mRemoteControlClient;
private ComponentName mFMMediaButtonIntentReceiver = null;
//添加以上
二、在alps\vendor\mediatek\proprietary\packages\apps\FmRadio\src\com\mediatek\fmradio下面添加一支文件:
文件名为:FMMediaButtonIntentReceiver.java
package com.mediatek.fmradio;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.util.Log;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import java.util.List;
public class FMMediaButtonIntentReceiver extends BroadcastReceiver {
public static final String TAG = "FmRx/Receiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, " mFMMediaButtonIntentReceiver onReceive");
String Iaction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(Iaction)) {
Log.d(TAG, " mFMMediaButtonIntentReceiver onReceive ACTION_MEDIA_BUTTON");
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return; }
int keycode = event.getKeyCode();
int action = event.getAction();
long eventtime = event.getEventTime();
Log.d(TAG, "onReceive keycode="+keycode+",action="+action);
String command = null;
switch (keycode) {
case KeyEvent.KEYCODE_MEDIA_STOP:
break;
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
command = FmRadioService.CMDTOGGLEPAUSE;
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
break;
/// M: AVRCP and Android Music AP supports the FF/REWIND @{
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
break;
default:
break;
}
if (command != null) {
if ((action == KeyEvent.ACTION_DOWN) && (event.getRepeatCount() == 0)) {
sendToStartService(context, command);}
if (isOrderedBroadcast()) {
abortBroadcast();}
}} }
public void sendToStartService(Context context, String command) {
Intent i = new Intent(context, FmRadioService.class);
i.putExtra(FmRadioService.CMD, command);
context.startService(i); }
}
三、在FmRadioService类的onCreate方法中添加如下:
@Override
public void onCreate() {
……
//添加以下
Log.d(TAG, "registerMediaButtonEventReceiver.mFMMediaButtonIntentReceiver" );
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName mFMMediaButtonIntentReceiver = new ComponentName(getPackageName(),
FMMediaButtonIntentReceiver.class.getName());
am.registerMediaButtonEventReceiver(mFMMediaButtonIntentReceiver);
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(mFMMediaButtonIntentReceiver);
PendingIntent pi = PendingIntent.getBroadcast(this /*context*/,
0 /*requestCode, ignored*/, i /*intent*/, 0 /*flags*/);
mRemoteControlClient = new RemoteControlClient(pi);
mAudioManager.registerRemoteControlClient(mRemoteControlClient);
int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY
| RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_STOP;
mRemoteControlClient.setTransportControlFlags(flags);
//添加以上
四、在FmRadioService类的onStartCommand方法中添加如下:
Log.d(TAG, ">>> FmRadioService.onStartCommand intent: " + intent + " startId: " + startId);
//添加如下
String cmd = intent.getStringExtra("command");
Log.d(TAG, ">>> FmRadioService.onStartCommand command: " + cmd);
if( CMDTOGGLEPAUSE.equals(cmd) ){
Log.d(TAG, "onStartCommand:CMDTOGGLEPAUSE" );
if(!mIsPowerUp){
powerUpAsync(FmRadioUtils.computeFrequency(getFrequency()));}
else{
powerDownAsync();}
}
//添加如上
五、在FmRadioService类的powerUpFm方法中添加如下:
private boolean powerUpFm(float frequency) {
……
mIsPowerUp = true;
setMute(true);
//添加以下
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
//添加以上
……
}
六、在FmRadioService类的powerDown方法中添加如下:
private boolean powerDown() {
……
if (!mIsPowerUp) {
Log.w(TAG, "Error: device is already power down.");
return true;
}
//添加以下
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
//添加以上
mIsPowerUp = false;
…… }
七、在FmRadioService类的onDestroy方法中添加如下:
public void onDestroy() {
……
abandonAudioFocus();
exitFm();
//添加以下
Log.d(TAG, "unregisterMediaButtonEventReceiver.mFMMediaButtonIntentReceiver" );
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.unregisterMediaButtonEventReceiver(mFMMediaButtonIntentReceiver);
mAudioManager.unregisterRemoteControlClient(mRemoteControlClient);
//添加以上 ……
}
八、修改alps\vendor\mediatek\proprietary\packages\apps\FmRadio\AndroidMainfest.xml,添加以下:
<receiver android:name="com.mediatek.fmradio.FMMediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>