Android的一条龙服务的时候退出程序 [英] Android stop service when exit the app

查看:94
本文介绍了Android的一条龙服务的时候退出程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建有​​背景音乐。游戏应用程序中使用Android的服务打 背景音乐,因为我想跑BGM而变化的活动。 我的问题是,我已经宣告结束()中的onPause方法在每个活动(我不想让用户返回和放大器;想杀死活动)。

所以当我打算到其他的活动,它调用的onDestroy并停止该服务。 我想停止该服务完全退出该应用程序(pressing home键),并想通过活动BGM和完成()中的onPause()。这可能吗?还是有其他解决方案?

 公共类BackgroundMusicService延伸服务{
    私有静态最后字符串变量= NULL;
    MediaPlayer的播放器;

    公众的IBinder onBind(意向为arg0){

        返回null;
    }

    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        球员= MediaPlayer.create(这一点,R.raw.topbgm);
        player.setLooping(真正的); //设置循环
        player.setVolume(100,100);
    }

    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
        player.start();
        返回1;
    }

    公共无效ONSTART(意向意图,诠释startId){
        // 去做

    }

    公众的IBinder onUnBind(意向为arg0){
        //做自动生成的方法
        返回null;
    }

    公共无效的onStop(){

    }

    公共无效的onPause(){

    }

    @覆盖
    公共无效的onDestroy(){
        player.stop();
        player.release();
        Log.i(服务,服务杀);
    }

    @覆盖
    公共无效onLowMemory(){

    }
}
 

在清单

 <服务机器人。BackgroundMusicService名称=安卓程序=:远程/>
 

解决方案

把这行YOUT活动

  stopService(新意图(这一点,BackgroundMusicService.class));
 

在的onDestroy()方法中,你pressing home键。

I'm creating a game application with background music.I used Android Service to play the background music because I wanted to run BGM while changing activities. my problem is,I have declared finish() in onPause method in each activity(I don't want to let the user to return & want to kill the activity).

so when I intent to other activity it calls onDestroy and stops the service. I want to stop the service exactly exit the app( pressing the home button )and want to go through activities with BGM and finish() in onPause().is this possible? or is there another solution?

public class BackgroundMusicService extends Service {
    private static final String TAG = null;
    MediaPlayer player;

    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.topbgm);
        player.setLooping(true); // Set looping
        player.setVolume(100, 100);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return 1;   
    }

    public void onStart(Intent intent, int startId) {
        // TO DO

    }

    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }

    public void onPause() {

    }

    @Override
    public void onDestroy() {
        player.stop();
        player.release();
        Log.i("service", "service killed");
    }

    @Override
    public void onLowMemory() {

    }
}

in manifest

<service android:name=".BackgroundMusicService" android:process=":remote" />

解决方案

put this line in yout activity

stopService(new Intent(this, BackgroundMusicService.class));

in onDestroy() method where you pressing the home button.

这篇关于Android的一条龙服务的时候退出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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