小编Sak*_*nis的帖子

如何在Android应用程序的后台播放音乐?

我希望在我的应用程序中,无论应用程序中发生什么,都可以播放音乐。我正在尝试使用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)

java service android background

3
推荐指数
1
解决办法
3614
查看次数

标签 统计

android ×1

background ×1

java ×1

service ×1