应用程序启动时绑定并启动服务 [英] Binding and starting a service when application starts

查看:287
本文介绍了应用程序启动时绑定并启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有一个服务,该服务封装了具有start方法的框架.该服务归结为这样的事情,许多事情省略了:

I have a service in Android that encapsulates a framework that has a start method. The service boils down to something like this, many things omitted:

public class MyService extends Service {

    private IBinder thisBinder;

    public MyService(){
        thisBinder = new LocalBinder();
    }

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

    public void start(Map<String, Object> options)
    {
        getDriverManager().start(options);
    }
}

我还有一个桥接类,可以调用该服务:

I also have a bridging class that makes calls to the service:

public class MyServiceBridge implements ServiceConnection {

    private boolean started = false;
    private boolean bound = false;
    private MyService myService;

    public MyServiceBridge(Context context){
        this.context = context;
    }

    public void bindService(){
        Intent intent = new Intent(getContext(), MyService.class);

        getContext().bindService(intent, this, getContext().BIND_AUTO_CREATE);
        getContext().startService(intent);
    }

    // Here's a sample call, and the one that is relevant
    public void start(Map<String, Object> options){
        setOptions(options);
        if(bound == true){
            getMyService().start(options);
        }
        else{
            started = true;
        }
    }
}

我调用网桥的start方法以运行服务.除了目前(在目前)这种特殊情况之外,这都可以正常工作. MyApplication类在onCreate上调用网桥的start方法:

I call the bridge's start method in order to run the service. This works fine, except in this particular situation (so far). The MyApplication class calls the bridge's start method on onCreate:

public class MyApplication extends Application {

    @Override
    public void onCreate() {

        super.onCreate();

        getServiceBridge().start(null);
    }
}

根据文档, 是在创建任何活动,服务或接收者对象(不包括内容提供者)之前,在应用程序启动时调用."确实如此,因为服务没有启动,而是在我关闭应用程序时启动(至少,奇数).如果将调用移至活动的onCreate方法,则此方法有效,但这并不理想,因为我也可以停止该服务,并且希望该服务在应用程序的生命期内运行.也就是说,该服务应在应用程序启动时启动,并在应用程序终止时停止.这有意义吗?还有另一种方法吗?

This, according to the docs is "Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.". Indeed it appears to be so, because the service does not start, and instead starts when I close the app (odd, at least). This works if I move the call to an activity's onCreate method, but that's not ideal because I can also stop the service, and I want the service to run for the lifetime of the app. That is, the service should start when the app starts and stop when the app terminates. Does this make sense? Is there another way to do this?

推荐答案

在我看来,当您决定在Application onCreate中运行服务时,您做得很好.但是当您关闭应用程序时听到服务已启动很奇怪. 我已经做过几次了,服务从必须调用的应用程序onCreate中启动.

In my opinion, you did a good job when you decided to run service in Application onCreate. but it is strange to hear that service is started when you close the app. I have done this several times, and service starts in application onCreate which must be called.

您是否检查过您的应用程序是否仍在运行并在后台运行?在测试之前,请确保您杀死了您的应用程序.使用Task Killer或类似的工具,确保始终重新启动应用程序.

Have you checked if your application is alive and run in background? Please make sure that you killed you application before testing. Use Task Killer or something like that, to be sure that application is always freshly started.

可悲的是,Android没有适当的机制可在退出应用程序时通知您,因为在系统决定终止该应用程序并释放资源之前,它仍然处于活动状态.

Sadly, Android does not have appropriate mechanism to notify you when application is exited, because it is still alive until system decides to kill it and free resources.

这篇关于应用程序启动时绑定并启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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