如果我从的onCreate在活动startService,完成()的活动,然后再次启动该应用程序会发生什么? [英] What happens if I startService from onCreate in Activity, finish() the Activity and then start the app again?

查看:165
本文介绍了如果我从的onCreate在活动startService,完成()的活动,然后再次启动该应用程序会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚的Andr​​oid是如何工作的,当涉及到生命周期的问题。

我决定有一个长期运行的服务来握住我的TCP的连接和其他的东西。

我有一个活动,StartupActivity。该活动启动的服务,然后我preSS一个按钮来完成的活动。然后,我再次启动相同的应用程序/活动,从而startService再次执行。

不过,我预计本服务还活着(至今没有所谓的onDestroy),但仍然在执行该服务中的onCreate法。

为什么?

第1步:在的onCreate是StartupActivit执行:

  @覆盖
保护无效的onCreate(android.os.Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.startup);    startService(新意图(StartupActivity.this,SamcomService.class));
    registerReceiver(connectionReceiver,新的IntentFilter(STARTUP_PROGRESS_MESSAGE));
    registerReceiver(connectionReceiver,新的IntentFilter(STARTUP_FINISH));    最后Button按钮=(按钮)findViewById(R.id.buttonExit);
    button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            完();
        }
    });
}

第2步:我preSS按钮,Activity.finish()被调用(我返回到主屏幕)

第3步:我再次启动应用程序,并为活动的的onCreate被多次执行,从而启动相同服务

现在,当我启动应用程序的服务应该运行第二次(我该如何检查?的)。我还没有看到,当服务的onDestroy被称为显示的敬酒,我也看到服务创建(和删除的onDestroy)通知。

然而,的onCreate被执行的第二时间。我现在有一个以上的服务在同一时间运行?


解决方案

  

然而,的onCreate被执行的第二时间。我现在有一个以上的服务在同一时间运行?


没有,我知道这是混乱的,但startService()方法不会启动该服务。它将在服务调用的每次你运行它。举例来说,如果服务没有启动,它会启动它,然后运行它。如果该服务已启动,它只是运行它。 Android的文件说,它的目的是通过这种方式,使得startService()方法是与您的服务进行通信的最简单的方法。


  

步骤2:我preSS按钮,Activity.finish()被调用(我返回到主屏幕)


这取决于您的服务在同一个进程,你的活动或没有运行。如果你有:工艺=在清单中的服务宣言东西,而你做了一切完美,服务是另一个进程,并有只有三个situiations inwhich您的服务将被销毁。 1,您的活动被销毁(几乎肯定无法发生,因为你的进程有其积极的活动优先); 2,有极低的内存,3个服务调用selfStop()。

由于完成()不会破坏你的应用(Android保持它在内存中包住用户回来它),你的服务或者是只暂停(如果是在同一进程)或仍在运行(如果是在一个不同的处理)。不管到startService另一个呼叫()将重新启动如果这是暂停和运行服务的startService方法。你只曾经有你的服务围绕之一。

I am trying to figure out how Android works when it comes to lifecycle issues.

I have decided to have a long-running Service to hold my TCP-connections and other stuff.

I have one Activity, StartupActivity. That Activity starts a service, and then I press a button to Finish the Activity. I then launch the same app/Activity again, and thus the startService is executed again.

However, I expected the Service to still be alive (there has been no onDestroy called), but the onCreate-method in the Service is still being executed.

Why?

STEP 1: onCreate in StartupActivit is executed:

@Override
protected void onCreate(android.os.Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startup);

    startService(new Intent(StartupActivity.this, SamcomService.class));
    registerReceiver(connectionReceiver, new IntentFilter("STARTUP_PROGRESS_MESSAGE"));
    registerReceiver(connectionReceiver, new IntentFilter("STARTUP_FINISH"));

    final Button button = (Button) findViewById(R.id.buttonExit);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });
}

STEP 2: I press the button, and Activity.finish() is called (I am returned to the home screen)

STEP 3: I launch the app again, and the onCreate for the Activity is once more executed, thus starting the same Service

Now, when I start the app the second time the Service should be running (how do I check that?). I havent seen the Toast that is displayed when the Service onDestroy is called, and I also see the Notification that the Service creates (and removes onDestroy).

However, the onCreate is executed a second time. Do I now have more than one Service running at the same time?

解决方案

However, the onCreate is executed a second time. Do I now have more than one Service running at the same time?

No, and I know this is confusing, but the startService() method does not start the service. It will be called in the service everytime you run it. For instance, if the service isn't started, it will start it and then run it. If the service is started, it will just run it. The android documentation says it was designed this way so that the startService() method is the easiest way to communicate with your service.

STEP 2: I press the button, and Activity.finish() is called (I am returned to the home screen)

This depends on if your service is running on the same process as your activity or not. If you have :process = "something" in your service declaration in the manifest, and you did everything else perfectly, the service is in another process and there are only three situiations inwhich your service will be destroyed. 1, your activity is destroyed(Almost guaranteed not to happen as your process has its activities active priority), 2, There is EXTREMELY low memory, 3 the service calls selfStop().

Since finish() does not destroy your app(Android keeps it around in memory encase the user comes back to it), your service is either just paused(If it was in the same process) or still running(If it was in a different process). Regardless another call to startService() will restart it if it was paused and run the service's startService method. You only ever have one of your services around.

这篇关于如果我从的onCreate在活动startService,完成()的活动,然后再次启动该应用程序会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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