Use Froyo registerMediaButtonReceiver API
This commit is contained in:
parent
a8bf7536ac
commit
a9db1f55fb
@ -31,7 +31,8 @@ public class MediaButtonReceiver extends BroadcastReceiver {
|
|||||||
public void onReceive(Context context, Intent intent)
|
public void onReceive(Context context, Intent intent)
|
||||||
{
|
{
|
||||||
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
|
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
|
||||||
if (MediaButtonHandler.getInstance().process(intent))
|
boolean handled = MediaButtonHandler.getInstance().process(intent);
|
||||||
|
if (handled && isOrderedBroadcast())
|
||||||
abortBroadcast();
|
abortBroadcast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,8 +78,11 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
|
|||||||
public void onResume()
|
public void onResume()
|
||||||
{
|
{
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if (ContextApplication.hasService())
|
if (ContextApplication.hasService()) {
|
||||||
ContextApplication.getService().userActionTriggered();
|
PlaybackService service = ContextApplication.getService();
|
||||||
|
service.userActionTriggered();
|
||||||
|
service.registerMediaButton();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean handleKeyLongPress(int keyCode)
|
public static boolean handleKeyLongPress(int keyCode)
|
||||||
|
@ -183,6 +183,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
private int mPendingSeek;
|
private int mPendingSeek;
|
||||||
private Song mLastSongBroadcast;
|
private Song mLastSongBroadcast;
|
||||||
public Receiver mReceiver;
|
public Receiver mReceiver;
|
||||||
|
public ComponentName mButtonReceiver;
|
||||||
public InCallListener mCallListener;
|
public InCallListener mCallListener;
|
||||||
private boolean mLoaded;
|
private boolean mLoaded;
|
||||||
/**
|
/**
|
||||||
@ -195,8 +196,23 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
*/
|
*/
|
||||||
private float mCurrentVolume = 1.0f;
|
private float mCurrentVolume = 1.0f;
|
||||||
|
|
||||||
private Method mStartForeground;
|
private static Method mStartForeground;
|
||||||
private Method mStopForeground;
|
private static Method mStopForeground;
|
||||||
|
private static Method mRegisterMediaButtonEventReceiver;
|
||||||
|
private static Method mUnregisterMediaButtonEventReceiver;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
mStartForeground = Service.class.getMethod("startForeground", int.class, Notification.class);
|
||||||
|
mStopForeground = Service.class.getMethod("stopForeground", boolean.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
mRegisterMediaButtonEventReceiver = AudioManager.class.getMethod("registerMediaButtonEventReceiver", ComponentName.class);
|
||||||
|
mUnregisterMediaButtonEventReceiver = AudioManager.class.getMethod("unregisterMediaButtonEventReceiver", ComponentName.class);
|
||||||
|
} catch (NoSuchMethodException nsme) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate()
|
public void onCreate()
|
||||||
@ -214,6 +230,9 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
|
|
||||||
ContextApplication.setService(this);
|
ContextApplication.setService(this);
|
||||||
|
|
||||||
|
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
|
||||||
|
mButtonReceiver = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
|
||||||
|
|
||||||
mLooper = thread.getLooper();
|
mLooper = thread.getLooper();
|
||||||
mHandler = new Handler(mLooper, this);
|
mHandler = new Handler(mLooper, this);
|
||||||
mHandler.sendEmptyMessage(CREATE);
|
mHandler.sendEmptyMessage(CREATE);
|
||||||
@ -229,9 +248,27 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
Toast.makeText(this, R.string.starting, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.starting, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request focus on the media buttons from AudioManager.
|
||||||
|
*/
|
||||||
|
public void registerMediaButton()
|
||||||
|
{
|
||||||
|
if (mRegisterMediaButtonEventReceiver != null) {
|
||||||
|
try {
|
||||||
|
mRegisterMediaButtonEventReceiver.invoke(mAudioManager, mButtonReceiver);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
Log.w("VanillaMusic", e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
Log.w("VanillaMusic", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart(Intent intent, int flags)
|
public void onStart(Intent intent, int flags)
|
||||||
{
|
{
|
||||||
|
registerMediaButton();
|
||||||
|
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
|
|
||||||
@ -292,6 +329,16 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
|
|
||||||
mLooper.quit();
|
mLooper.quit();
|
||||||
|
|
||||||
|
if (mUnregisterMediaButtonEventReceiver != null) {
|
||||||
|
try {
|
||||||
|
mUnregisterMediaButtonEventReceiver.invoke(mAudioManager, mButtonReceiver);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
Log.w("VanillaMusic", e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
Log.w("VanillaMusic", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
unregisterReceiver(mReceiver);
|
unregisterReceiver(mReceiver);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@ -357,16 +404,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
mMediaPlayer.setOnCompletionListener(this);
|
mMediaPlayer.setOnCompletionListener(this);
|
||||||
mMediaPlayer.setOnErrorListener(this);
|
mMediaPlayer.setOnErrorListener(this);
|
||||||
|
|
||||||
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
|
|
||||||
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
try {
|
|
||||||
mStartForeground = getClass().getMethod("startForeground", int.class, Notification.class);
|
|
||||||
mStopForeground = getClass().getMethod("stopForeground", boolean.class);
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
Log.d("VanillaMusic", "falling back to pre-2.0 Service APIs");
|
|
||||||
}
|
|
||||||
|
|
||||||
SharedPreferences settings = getSettings();
|
SharedPreferences settings = getSettings();
|
||||||
settings.registerOnSharedPreferenceChangeListener(this);
|
settings.registerOnSharedPreferenceChangeListener(this);
|
||||||
mNotificationMode = Integer.parseInt(settings.getString("notification_mode", "1"));
|
mNotificationMode = Integer.parseInt(settings.getString("notification_mode", "1"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user