我希望在我的应用程序中,无论应用程序中发生什么,都可以播放音乐。我正在尝试使用Service. 这是MyService.class
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MyService extends Service implements MediaPlayer.OnPreparedListener {
MediaPlayer mMediaPlayer = null;
private static final String ACTION_PLAY = "com.example.action.PLAY";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(ACTION_PLAY)) {
mMediaPlayer = MediaPlayer.create(this, R.raw.theme_song);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.prepareAsync(); // prepare async to not …Run Code Online (Sandbox Code Playgroud)