Android服务无法正常启动

时间:2014-02-05 19:28:44

标签: android service background

我实现了我在这里找到的所有内容,但我的服务还没有开始。

我需要做的是在用户点击按钮时开始播放广播电台,并且只有在再次点击它时才停止播放。如果应用程序关闭,我不希望停止收音机。

当我测试我发送的代码时,没有任何反应。

清单:

<service
    android:name=".RadioService"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:exported="false"
>
</service>

服务代码:

package com.po.portelaonline.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

    public class RadioService extends Service {

    Player player;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return Service.START_NOT_STICKY;
    }

    @Override
    public void onCreate(){
        player = new Player();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy(){
        player.stopThread();
    }

    }

玩家类代码:

package com.po.portelaonline.service;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;

import com.po.portelaonline.classes.Util;

class Player extends Thread {

volatile boolean running = true;

@Override
public void run() {

    MediaPlayer player = new MediaPlayer();

    try {

        player.setDataSource(Util.getRadioUrl());
        player.setOnPreparedListener(oplPlayer);
        player.prepareAsync();

        while (running) {
            Player.sleep(500);
        }

        player.release();
        player = null;

    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void stopThread() {
    running = false;
}

OnPreparedListener oplPlayer = new OnPreparedListener() {

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

}

我这样称呼:

Intent i = new Intent(getApplicationContext(), RadioService.class);
if(getApplicationContext().startService(i) == null){
findViewById(R.id.btnPlayStop).setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.stop));
}else{
getApplicationContext().stopService(i);
findViewById(R.id.btnPlayStop).setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.play));
}

2 个答案:

答案 0 :(得分:0)

尝试使用此代码进行服务类。

您无需在单独的线程中创建MediaPlayer对象。 player.prepareAsync();已经异步。

修改

进入AndroidManifest.xml的RadioService类的路径应该是绝对的:android:name="com.po.portelaonline.service.RadioService"

public class RadioService extends Service implements OnPreparedListener, OnErrorListener{

    MediaPlayer mPlayer;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startPlayer();
        return Service.START_NOT_STICKY;
    }

    @Override
    public void onCreate(){
        initMediaPlayer();
    }


    private void initMediaPlayer () {
        mPlayer = new MediaPlayer();
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnErrorListener(this);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setWakeMode(getApplicationContext(),
                PowerManager.PARTIAL_WAKE_LOCK);
    }

    private void startPlayer(){
        player.setDataSource(Util.getRadioUrl());
        player.prepareAsync();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

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

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

    @Override
    public boolean onError (MediaPlayer mp, int what, int extra) {
        Log.e("RadioService OnError", "what="+what+" extra="+extra);
        return false;
    }

}

答案 1 :(得分:0)

我猜它应该是 if(getApplicationContext().startService(i) != null) 而不是if(getApplicationContext().startService(i) == null)

启动后,您将立即停止服务。