服务意图必须明确

时间:2015-03-04 09:06:25

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

我试图将服务用于背景音乐。

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>

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

2 个答案:

答案 0 :(得分:2)

请从意图中调用该服务并设置该意图的操作,如下所示:

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

由于

答案 1 :(得分:1)

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

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

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