我有一个播放音乐的服务,我需要它显示一个通知"播放","暂停"并且"停止"纽扣。我的服务代码是:
public class RadioService extends Service implements MediaPlayer.OnPreparedListener {
MediaPlayer radioPlayer;
String url = "radiourl";
String action;
public RadioService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.e("Service", "Bind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("Service", "onStart");
action = intent.getAction();
Log.e("ServiceAction", action);
switch (action) {
case "play":
radioPlayer = new MediaPlayer();
radioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
radioPlayer.setDataSource(url);
} catch (IOException e) {
Log.e("setDataSource", e.toString());
}
radioPlayer.prepareAsync();
radioPlayer.setOnPreparedListener(this);
}
return START_STICKY;
}
@Override
public void onPrepared(MediaPlayer radioPlayer) {
Log.e("Service", "onPrepared");
radioPlayer.start();
}
@Override
public void onDestroy() {
Log.e("Service", "onDestroy");
if(radioPlayer != null && radioPlayer.isPlaying()) {
radioPlayer.stop();
radioPlayer.reset();
radioPlayer.release();
}
super.onDestroy();
}
}
我的通知必须与API 16及更高版本兼容。我希望通知按钮向我的服务发送一个动作,这将在我的切换指令中执行一个正确的大小写。这甚至可能吗?我的服务应该如何?