Android的服务:它应该被声明为一个过程或者没有? [英] Android Service: should it be declared as a process or not?

查看:119
本文介绍了Android的服务:它应该被声明为一个过程或者没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个简单的Andr​​oid 服务,默认情况下,在同一个进程,我的应用程序/ APK中部署。我想要的服务,每个活动同时运行。要做到这一点,在每个 Activity.onStart() Activity.onStop()的实施,我有逻辑调用 Activity.bindService() Activity.unbindService(),分别为。

I have implemented a simple Android Service that, by default, is deployed within the same process as my app / apk. I want the Service running concurrently with each Activity. To make that happen, in each Activity.onStart() and Activity.onStop() implementation, I have logic that invokes Activity.bindService() and Activity.unbindService(), respectively.

好吧,这一切工作正常,但感觉很尴尬。是否有任何其他的方式来确保服务是无需连续运行并绑定到活动重新调用 Activity.bindService() Activity.unbindService()每个活动?如果服务在这种情况下被宣布为一个独立的进程?

Well, all of this works fine, but it feels awkward. Is there any other way to make sure the Service is continuously running and bound to all Activities without having to re-invoke Activity.bindService() and Activity.unbindService() for each Activity? Should the Service in this case be declared as a stand-alone process?

另外,我的服务启动一个单独的线程,但从来没有停止。如果我的code停止线程?有机会的线程可能会被孤立?启动/停止螺纹 OnUnbind / OnRebind 似乎有点小题大做。

Also, my Service starts a separate thread, but never stops it. Should my code stop the thread? Is there a chance the thread could be orphaned? Starting / stopping the thread with OnUnbind / OnRebind seems like overkill.

推荐答案

创建基础活动并调用 bindService 在onStart unbindService 的onStop

Create a base Activity and call bindService in onStart, unbindService in onStop.

public class BaseActivity extends Activity {

    @Override  
    public void onStart() {
        // ...
        bindService(intent, serviceConnection, flags);
    }

    @Override
    public void onStop() {
        // ....
        unbindService(serviceConnection);    
    }
}

这将确保每一个扩展底座的活动被绑定到服务。

This will make sure every activity that extends base is bound to the service.

在最后一项活动是从服务绑定,这将被停止。如果你想避免这种情况,叫 startService ,然后再绑定到它。这将从停止,即使你没有运行的活动prevent服务。

When last activity is unbound from the service, it will be stopped. If you want to avoid that, call startService first, and then bind to it. This will prevent service from stopping even if you don't have running activities.

如果在这种情况下,服务被宣布为一个独立的进程?

Should the Service in this case be declared as a stand-alone process?

在你的情况,你不需要为您服务的独立进程。

In your case, you don't need a separate process for your service.

另外,我的服务启动一个单独的线程,但从来没有停止。如果我的code停止线程?

Also, my Service starts a separate thread, but never stops it. Should my code stop the thread?

如果你要停止你的服务,你应该停止线程,因为线程是一个GC根,并从它访问的所有对象将保留在内存中。所以,就不会使用无限的线程是内存泄漏。

If you want to stop your service, you should stop your thread because thread is a GC root, and all objects accessible from it will remain in memory. So, infinite thread that is not used is a memory leak.

您可以根据您的要求执行的线程不同的方式。您可以实现在常规线程你的服务的ThreadPoolExecutor 处理程序。选择适合您需求的解决方案。

You can implement threading different ways depending on your requirements. You can either implement a regular thread in your Service, or a ThreadPoolExecutor or a Handler. Pick a solution that fits to your needs.

这篇关于Android的服务:它应该被声明为一个过程或者没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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