Android的后台服务 [英] Android background service

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

问题描述

我读通过文档和更多的时间,我花我得到更多的困惑是什么来完成我想要做的最简单的方法。我想写一个简单的服务,这在开始按钮的onClick,并结合活动。而当活动结束以后再次启动(不仅是重新启动!),我要检查服务是否已经运行,并绑定到它。我该怎么做呢?

I'm reading through the docs and the more time I spend I get more confused what's the easiest way to accomplish what I'm trying to do. I want to write a simple Service, which starts at button onClick and binds to the activity. And when the activity is closed and started again later (not only restarted!), I want to check whether the service is already running and bind to it. How do I do it?

感谢

推荐答案

如果你开始一个Android服务与startService(..),该服务将继续运行,直到你明确地调用stopService(..)。有两个原因一个服务可以被系统上运行。如果有人致电Context.startService(),则系统将检索服务(创建它,并调用它的onCreate(),如果需要的方法),然后调用其onStartCommand(意向,INT,INT)方法与客户端提供的参数,该服务将在这一点上继续运行,直到Context.stopService()或stopSelf()被调用。注意,多次调用Context.startService()不能嵌套(尽管他们导致多个相应调用onStartCommand()),所以无论多少次启动一个服务被调用将被停止一次Context.stopService()或stopSelf();然而,服务可以使用他们的stopSelf(int)的方法,以确保该服务是不停止,直到开始的意图都被处理。

"If you start an android Service with startService(..) that Service will remain running until you explicitly invoke stopService(..). There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

客户端也可以使用Context.bindService()获取到服务的持续连接。这也造成了服务,如果它没有运行(调用的onCreate(),而这样做),但不调用onStartCommand()。客户端将接收到的IBinder对象从它的onBind(意向)方法的服务返回,允许客户机然后拨打电话回服务。该服务将保持运行,只要建立连接(该客户机是否保留在服务的​​的IBinder基准)。通常的IBinder返回为已写入在AIDL复杂接口

Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a complex interface that has been written in aidl.

一个服务可以同时起步,并没有绑定到它的连接。在这种情况下,系统将保持服务,只要任一被启动或有一个或多个连接到其与Context.BIND_AUTO_CREATE标志运行。一旦没有这些情况成立,该服务的的onDestroy()方法被调用并在服务有效地终止。所有清理(停止线程,注销接收器)应在从的onDestroy()返回的是完整的。

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy()."

您可以检查服务是否运行:

you can check if service is running or not:

public boolean isServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.example.app.ServiceClassName".equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

这篇关于Android的后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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