服务意图必须明确

Service Intent must be explicit

我正在尝试使用背景音乐服务。

package com.example.neotavraham;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;


public class PlayMusicService extends Service implements MediaPlayer.OnPreparedListener {
  public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
  MediaPlayer mMediaPlayer = null;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(ACTION_PLAY)) {
      mMediaPlayer = MediaPlayer.create(this, R.raw.yedid_nefesh);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    return flags;
  }

  @Override
  public void onPrepared(MediaPlayer mp) {
    mp.start();
  }

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

我从 MainActivity 调用它,行是:startService(new Intent(PlayMusicService.ACTION_PLAY));

当然我添加了一个 intent-filter

<intent-filter>
  <action android:name="com.example.neotavraham.PLAY"/>
</intent-filter>

我在互联网上寻找一个很好的解决方案,但找不到...我该怎么办?

请从 intent 调用服务并像这样设置该 intent 的操作:

Intent i = new Intent(this,PlayMusicService.class);
i.setAction("com.example.neotavraham.PLAY");
startService(i);

这段代码可以工作。

启动媒体播放器的后台服务

package com.example.neotavraham;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;


public class PlayMusicService extends Service  {
  public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
  MediaPlayer mMediaPlayer = null;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(ACTION_PLAY)) {

      mMediaPlayer = MediaPlayer.create(this, R.raw.idil);
        mMediaPlayer.setLooping(true); // Set looping
        mMediaPlayer.setVolume(100,100);
        mMediaPlayer.start();
    }
    return flags;
  }

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

    @Override
    public void onDestroy() {
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }
}

谢谢

在您的 activity 中执行以下操作:

Intent i = new Intent(this,PlayMusicService.class);
i.putExtra("action","com.example.neotavraham.PLAY");
startService(i);

在您的服务中执行以下操作:

package com.example.neotavraham;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;


public class PlayMusicService extends Service  {
  public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
  MediaPlayer mMediaPlayer = null;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getStringExtra("action").equals(ACTION_PLAY)) {
      mMediaPlayer = MediaPlayer.create(this, R.raw.yedid_nefesh);
      mMediaPlayer = MediaPlayer.create(this, R.raw.idil);
        mMediaPlayer.setLooping(true); // Set looping
        mMediaPlayer.setVolume(100,100);
        mMediaPlayer.start();
    }
    return flags;
  }

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

    @Override
    public void onDestroy() {
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }
}

在您的清单中执行以下操作:

<service
            android:name="com.example.neotavraham.PlayMusicService"
           />

这是您应用的完美代码。希望这最终会奏效....