反正是有创建将运行与否的应用程序创建它仍在运行一个后台程序服务? [英] Is there anyway to create a background process service that will run whether or not the app created it is still running?

查看:160
本文介绍了反正是有创建将运行与否的应用程序创建它仍在运行一个后台程序服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想我的应用程序创建一个最初将在后台运行,并做一些东西的服务。这个后台进程将永远不会停止。它会不断地继续运行。后台进程可以创建或破坏的唯一途径是通过应用程序。据我所知,有无限的可能性杀死一个过程。我想我想我的应用程序能告诉我,这个过程是否运行和检索这一过程中的实时信息。一起的是,能够启动和/或破坏的后台进程。

So, I want my app to create a Service that will initially run in the background and do some stuff. This background process will never stop. It will constantly keep running. The ONLY way the background process can be created or destroyed would be through the app. I understand that there are endless possibilities to kill a process. I guess I want my app to be able to tell me whether or not the process is running and retrieve real time information from that process. Along with that, be able to start and/or destroy the background process.

所以,让我们说,你要打开的应用程序和启动/创建服务。即使你关闭/终端/调用的onDestroy的应用程序,我仍然希望后台进程运行。摧毁该服务的唯一方法是重新开启/重新创建应用程序,并摧毁它。

So, let's say that you were to open the app and start/create the service. Even when you close/terminal/call onDestroy for the app, I still want the background process to be running. The only way to destroy this service would be to re-open/re-create the app, and destroy it.

Android版是否允许这样的事情?有没有办法来解决这个问题?

Does Android allow something like this? Is there a way to get around this?

我要创建一个 IntentService 并使其运​​行一个无限循环。虽然,我唯一的问题是如何获得从 IntentService 信息。从我了解到,一个 IntentService 创建并杀死自己当它这样做。

I was going to create a IntentService and make it run an infinite loop. Though, my only problem is how to obtain information from the IntentService. From what I've learned, an IntentService is created and kills itself when it's done.

我还是新到Android,所以不要犹豫要具体和/或补救。

I'm still new to Android, so don't hesitate to be specific and/or remedial.

推荐答案

您不需要做2应用的,其实,如果你的要求是只能停止/从主应用程序会创建服务只是过于复杂的事情(如你不需要AIDL,因为在所有的都在同一个应用程序),这也适用于上述答案(不能回复呢!)。

You dont need to make 2 apps for that, in fact, if your requirement is to only be able to stop/create the service from the main app it will only overcomplicate things (such as that you dont need aidl at all since its all in the same app) , that goes for the above answer (cant reply yet!).

刚甚至在同一个包的其他活动,创建应用程序和服务类。然后在清单XML的应用程序节点注册服务是这样的:

Just create the app and the service class even on the same package as the other activities. Then on the manifest xml register the service like this at the application node:

<service
        android:name="com.example.yourpackage.Service"
        android:process=":remote"> 
        <intent-filter>
            <action
                android:name="com.example.yourpackage.Service" />
        </intent-filter>
    </service>

你的Andr​​oid做什么:工艺=:远程标签明确设置在不同的进程中运行的服务(由远程的名称),比该应用程序的其余部分,同时还成为其中的一部分

What you are doing with the android:process=":remote" tag is explicitly setting the service to run in a different process (by the name of 'remote') than the rest of the app while still being part of it.

然后,要启动和停止它只是使用这些从你的活动:

Then, to start and stop it simply use these from your activity:

startService(new Intent(this, Service.class));

·
·
·

· · ·

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

请确保您阅读服务(和broadcastreceivers)反正文档,但是会给你一个总体思路到哪里的目标,什么不该做的过于复杂。

Make sure you read the documentation about services (and broadcastreceivers) anyway, but that will give you a general idea as to where to aim and what NOT to do to overcomplicate.

我一直与最近的服务对此事这样的IM新鲜的,如果你有任何问题,让我知道这里!

I've been working with services lately so im fresh on the matter, if you have any questions let me know here!

&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;编辑:>>>>>>>>>>>>>>>

嗯,我想我让你现在..如果你不想在应用程序完成后忘掉的服务继续运行:远程一部分。这样,他们都将共享相同的应用程序的生命周期(而不是因为他们有不同的人上下文的生命周期),对不起,如果这部分让你感到困惑,并感谢投票!

Ah I think I get you now.. if you dont want the service to keep running after the "app" is finished just forget about the :remote part. That way they will both share same app lifecycle (but not context lifecycle as they have different ones), sorry if that part confused you and thanks for the vote!.

这篇关于反正是有创建将运行与否的应用程序创建它仍在运行一个后台程序服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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