从应用开始,而不是活动推出服务 [英] Launch service from app start, not activity

查看:102
本文介绍了从应用开始,而不是活动推出服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发动服务当应用程序启动而不是活动;然后说服务将推出的活动。我需要做的这一点,因为我的应用程序必须始终在运行,当我总是说我的意思是始终。而唯一的办法我已经成功地避免了操作系统杀死了我的应用程序是通过启动服务为粘稠,应该杀的Andr​​oid无论是我还是活动我的服务,我会重新启动它们的时候了。

I want to launch a Service when the app is launched instead of an Activity; and then said Service will launch an Activity. I need to do this because my app needs to be running ALWAYS, and when I say ALWAYS I mean ALWAYS. And the only way I've managed to avoid the OS killing my app is by starting a service as Sticky and should Android kill either my Activity or my Service I'll restart them right away.

我发现这个<一个href=\"http://stackoverflow.com/questions/4467600/how-to-launch-a-android-service-when-the-app-launches\">question但上方的答案似乎相当笨拙,任何一个有一个更好的主意吗?

I found this question but the top answer seems rather clumsy, any one has a better idea?

PS:我知道这看起来并不像一个非常友好的应用程序,但这个是一个非常具体的研究方案,它不是针对普通用户,即电话仅用于这一目的;但即使内存是专门为我的应用程序的Andr​​oid不断飘飞杀死它...我可能有关于Android的本意是严格的内存管理方案的任何问题,现在都没有了。

PS: I know this doesn't look like a very friendly app but this is a very specific research scenario and it's not intended for regular users, i.e. the phone is solely used for this purpose; but even if memory is dedicated to my app Android keeps killing it every now and then... Any doubts I might have had about Android's purported strict memory management scheme are now gone.

推荐答案

您似乎不明白这个问题。一般来说活动并没有表现出任何的用户界面 - 它通常不会,但它不是强制性的。所以,你可以应用的出发点简单地设置你的隐形的活动。和无形的方式,无论主题为

You seem to not understand the subject. In general Activity does NOT have to show any UI - it usually does but it is NOT mandatory. So you can simply set app's starting point to your "invisible" activity. And invisible means either themed as

android:theme="@android:style/Theme.NoDisplay"

或只是您的code不会做任何的setContentView(),一旦它的任务是在你的的onCreate(),启动另一个活动,并终止这一次与完成() - 无UI会从活动弹出 - 这样你可以很容易地从做有益于你的作业活动的子类(这可能对于某些任务更简单),仍然不需要任何用户界面:

or simply your code will not do any setContentView() and once it's job is done in your onCreate(), you start another activity and terminate this one with finish() - and no UI would pop up from that activity - that way you can easily benefit from doing your job in activity subclass (which may be simpler for some tasks) and still do not need any UI:

public void onCreate(Bundle bundle) {
   super.onCreate(bundle);

   // [... do your job here...]

   // we're done, so let's jump to another acitivity
   // this can be skipped if you do not want to jump anywhere

   Intenet intent = new Intent(....)
   ...

   try {
      startActivity( intent );

      // finish him
      finish();

   } catch ( Exception e ) {
      e.printStackTrace();
   }

}

这篇关于从应用开始,而不是活动推出服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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