应用程序与寡妇分离时,带有Mediaplayer的Android服务被破坏 [英] Android service with mediaplayer destroyed when app detached from widow

查看:77
本文介绍了应用程序与寡妇分离时,带有Mediaplayer的Android服务被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在后台服务中播放音乐.一切正常,除非我从最近的应用程序列表中删除了该应用程序,但该服务已被破坏.当没有mediaplayer时,我用来使服务正常运行的方法很不错.这是我的代码

I am streaming music in a background service. Everything works fine except when I remove the app from recent app list the service destroyed . The approach I used to make the service run as sticky work fine when there is no mediaplayer . Here is my code

public class StreamListenerService extends Service implements MediaPlayer.OnPreparedListener,
        MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {

    public static MediaPlayer mMediaPlayer;
    private String streamUrl;
    public static boolean isPlaying = false;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        streamUrl = intent.getStringExtra("streamUrl");



        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.reset();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


        try {
            mMediaPlayer.setDataSource(streamUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }


        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnCompletionListener(this);

        isPlaying = true;

        return Service.START_STICKY_COMPATIBILITY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.d("StreamListenerService", "onDestroy");
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
            mMediaPlayer.reset();
            isPlaying = false;
        }
        super.onDestroy();

    }


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

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mMediaPlayer.start();
        isPlaying = true;
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

        Toast.makeText(this, R.string.text_problem_in_playing_audio_stream, Toast.LENGTH_SHORT).show();

        mMediaPlayer.stop();
        mMediaPlayer.reset();

        //Toast.makeText(this, R.string.text_problem_in_playing_stream,Toast.LENGTH_SHORT).show();

        this.onDestroy();
        return false;
    }


    @Override
    public void onCompletion(MediaPlayer mp) {
        Toast.makeText(this, R.string.text_stream_finished, Toast.LENGTH_SHORT).show();
        mMediaPlayer = mp;
        mMediaPlayer.stop();
        mMediaPlayer.reset();
        this.onDestroy();
    }
}

推荐答案

您需要通过添加属性 android:process =:whatever" 到服务标签.这样可以确保在您的应用程序的主要流程为正常运行时,即从应用程序列表中删除该应用程序时,该服务不会被终止.

You need to specify that the service runs in its own process in your app manifest, by adding the attribute android:process=":whatever" to the service tag. This will ensure that the service isn't killed when your app's main process is, i.e. when the app is removed from the apps list.

如果要在用户停止之前一直运行服务,请作为前台服务启动它.您可以通过在服务中调用 startForeground 来完成此操作.您需要将一个通知ID(您自己选择的整数)和一个通知传递给startForeground.通知将保留在任务栏中,直到用户停止服务为止.当然,您应该在通知中或在通知的其中一项操作中提供一个PendingIntent来停止服务.

If you want to make a service run until the user wants to stop, start it as a foreground service. You do this by calling startForeground in the service at some point. You need to pass a notification id (integer of your own choosing) and a Notification to startForeground. The notification will stay in the task bar until the user stops the service. You should, of course, provide a PendingIntent to stop the service in the notification or in one of the notification's actions.

这篇关于应用程序与寡妇分离时,带有Mediaplayer的Android服务被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆