【问题标题】:Service Intent must be explicit服务意图必须是明确的
【发布时间】:2015-03-04 09:06:24
【问题描述】:

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

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>
  <action android:name="com.example.neotavraham.PLAY"/>
</intent-filter>

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

【问题讨论】:

标签: android android-intent android-service android-mediaplayer


【解决方案1】:

请从 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();
    }
}

谢谢

【讨论】:

  • 好的,错误消失了,但音乐没有开始,这是为什么呢?
  • 一切都很好,你遇到了什么错误??也请使用调试器检查问题出在哪里?
  • 请检查我编辑的答案并尝试使用代码。它必须工作
  • 试过了,没用,奇怪的是它连onStartCommand方法里面的if语句都没有,我用logcat检查过
  • 你在 intent.getAction() 中得到了什么?
【解决方案2】:

在您的活动中执行以下操作:

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"
           />

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

【讨论】:

  • 成功了!非常感谢!最后一件事,我不想在另一个线程上处理这个,所以我尝试使用 prepareAsync(),如我的代码所示,但它给出了“java.lang.IllegalStateException”错误
  • 由于服务已经在后台,您不需要使用另一个线程...
猜你喜欢
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
相关资源
最近更新 更多