按下Home键后,安卓服务在运行[英] Android service running after pressing Home key

本文是小编为大家收集整理的关于按下Home键后,安卓服务在运行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个Android服务,在使用startservice()的应用程序的第一个活动的oncreate中创建.我需要在应用程序的整个寿命中运行此服务,即,应用程序中的所有活动.但是,在用户按下Home键或后按钮后,服务不应使用资源.除了在所有活动的oppause()方法中停止该服务,是否有任何优雅的方法?

推荐答案

您可以执行Darrell所建议的,但将该代码放在一个扩展活动的新类中,然后在所有正常活动中扩展.

我不知道任何其他更优雅的方式来实现目标.

其他推荐答案

而不是使用startservice,您可以在OnResume和UnbindService中调用BindService.当没有打开的绑定时,您的服务将停止.

您需要创建一个ServiceConnection以访问服务.例如,这是一个嵌套在myservice中的类:

class MyService {
    public static class MyServiceConnection implements ServiceConnection {
        private MyService mMyService = null;

        public MyService getMyService() {
            return mMyService;
        }
        public void onServiceConnected(ComponentName className, IBinder binder) {
            mMyService = ((MyServiceBinder)binder).getMyService();
        }
        public void onServiceDisconnected(ComponentName className) {
            mMyService = null;
        }
    }

    // Helper class to bridge the Service and the ServiceConnection.
    private class MyServiceBinder extends Binder {
        MyService getMyService() {
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return false;  // do full binding to reconnect, not Rebind
    }

    // Normal MyService code goes here.
}

可以使用此辅助级访问服务:

MyServiceConnection mMSC = new MyService.MyServiceConnection();

// Inside onResume:
bindService(new Intent(this, MyService.class), mMSC, Context.BIND_AUTO_CREATE);

// Inside onPause:
unbindService(mMSC);

// To get access to the service:
MyService myService = mMSC.getMyService();

本文地址:https://www.itbaoku.cn/post/102734.html

问题描述

I have an Android service, created in OnCreate of first Activity of the application using StartService(). I need this service to be running throughout the life span of the application ie, all the activities in the application. But the service should not consume the resources after user pressed Home key or Back button. Is there any elegant way to do that other than stopping the service in onPause() method of all the activities?

推荐答案

You could do what Darrell suggests but put that code in a new class that extends Activity and then extend that on all your normal Activities.

I don't know any other more elegant way of achieving your goals.

其他推荐答案

Instead of using StartService, you can call bindService in onResume and unbindService in onPause. Your service will stop when there are no open bindings.

You'll need to create a ServiceConnection to get access to the service. For instance, here's a class nested inside MyService:

class MyService {
    public static class MyServiceConnection implements ServiceConnection {
        private MyService mMyService = null;

        public MyService getMyService() {
            return mMyService;
        }
        public void onServiceConnected(ComponentName className, IBinder binder) {
            mMyService = ((MyServiceBinder)binder).getMyService();
        }
        public void onServiceDisconnected(ComponentName className) {
            mMyService = null;
        }
    }

    // Helper class to bridge the Service and the ServiceConnection.
    private class MyServiceBinder extends Binder {
        MyService getMyService() {
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return false;  // do full binding to reconnect, not Rebind
    }

    // Normal MyService code goes here.
}

One can use this helper class to get access to the service via:

MyServiceConnection mMSC = new MyService.MyServiceConnection();

// Inside onResume:
bindService(new Intent(this, MyService.class), mMSC, Context.BIND_AUTO_CREATE);

// Inside onPause:
unbindService(mMSC);

// To get access to the service:
MyService myService = mMSC.getMyService();