0

我有一个播放音乐的 android 服务。我使用以下代码开始我的主要活动:

Intent service = new Intent(MainMenu.this, musicservice.class);
MainMenu.this.startService(service); 

这是我的服务:

public class musicservice extends Service {
@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    MediaPlayer mp;
    mp = MediaPlayer.create(musicservice.this, R.raw.music);
    mp.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });   
    mp.start();

    return Service.START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

}

它 stsrts 工作正常,但是当我开始另一个活动时,我的音乐响起,似乎我的服务被破坏了!但不想要这个,我希望我的服务只在我的应用程序结束时停止。音乐仅在用户使用应用程序时播放。即使应用程序在后台运行,我也不希望我的音乐播放!我怎么能实现这个?

4

2 回答 2

1

如果您希望您的应用程序在您的活动开启时播放音乐,请尝试绑定它

它应该看起来像这样:

服务:

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;

import com.example.playmusic.R;

public class PlayMusicService extends Service {

    private final IBinder binder = new LocalBinder();

    private MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class LocalBinder extends Binder {

        public PlayMusicService getService() {
            return PlayMusicService.this;
        }
    }

    public void play() {
        player = MediaPlayer.create(this, R.raw.music);
        player.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
        player.start();

    }

    public void pause() {
        player.pause();
    }
}

活动:

package com.example.playmusic;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.example.service.PlayMusicService;
import com.example.service.PlayMusicService.LocalBinder;

public class MainActivity extends Activity {

private PlayMusicService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onPause() {
    super.onPause();
    service.pause();
}

@Override
protected void onStart() {
    super.onStart();
    if (service != null) {
        service.play();
    } else {
        bindService();
    }
}

    private void bindService() {
        bindService(new Intent(this, PlayMusicService.class), new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                service = null;

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                LocalBinder localBinder = (LocalBinder) binder;
                service = localBinder.getService();
                service.play();

            }
        }, Context.BIND_AUTO_CREATE);
    }

}
于 2013-03-29T17:59:34.423 回答
0

当您从活动启动服务时,您的活动销毁时服务销毁。

用于AlarmManager重复安排服务。

于 2013-03-29T17:14:09.923 回答