简体   繁体   English

我如何知道何时启动和销毁Android应用程序?

[英]How do I know when an Android app is launched and destroyed?

I need to run some code when an App is launched and when it completely exits (is destroyed). 我需要在启动应用程序并完全退出(销毁)时运行一些代码。 I am using onCreate and onDestroy but unfortunately screen orientation changes destroys and recreates the activity. 我正在使用onCreate和onDestroy,但是不幸的是,屏幕方向更改会破坏并重新创建活动。

Take a look at Android Application http://developer.android.com/reference/android/app/Application.html 看看Android应用程序http://developer.android.com/reference/android/app/Application.html

You can know when Application created (Application.onCreated) 您可以知道何时创建应用程序(Application.onCreated)

BUT you can't know when Application destroyed. 但是您不知道应用程序何时被破坏。 Application.onTerminate() is only for use in emulated process environments. Application.onTerminate()仅用于仿真流程环境。

Every time the app launched the onCreate method of your launcher activity is called. 每次应用启动时,都会调用启动程序活动onCreate方法。 So you have to put the desired code in onCreate method of the Launcher activity. 因此,您必须将所需的代码放入Launcher活动的onCreate方法中。

And to detect whether your app is destroyed or not you can override onDestroy method ( just like adding the onCreate ) from Override unimplemented method menu. 并且要检测您的应用程序是否被破坏,您可以从“ 覆盖未实现的方法”菜单中override onDestroy方法(就像添加onCreate )。 And put the codes inside of this method that you want to be executed when the app will be destroyed. 并将代码放入要销毁应用时要执行的此方法中。

You could try this, if your code can happen in the background (won't work if you need something to happen on the UI Thread): 如果您的代码可以在后台发生,则可以尝试这样做(如果您需要在UI线程上进行某些操作,则无法使用):

Have your activity hold a reference to a thread you extended, let's call it MyThread operator. 让您的活动持有对您扩展的线程的引用,我们将其称为MyThread运算符。

In onCreate, do that: 在onCreate中,执行以下操作:

if(operator==null){
    operator = new MyThread();
    operator.start();     
}

Then, in MyThread, put your code in the two methods run() and interrupt() . 然后,在MyThread中,将代码放入两个方法run()interrupt()

When your activity gets recreated through orientation change, it will keep the operator refence to your background thread. 当通过方向更改重新创建您的活动时,它将使操作员能够参考您的后台线程。 When you actually close your application, android will terminate your operator after some seconds and therefore execute your code. 当您实际关闭应用程序时,android将在几秒钟后终止您的运算符,并因此执行您的代码。

Someone correct me please if I'm wrong, I haven't slept too long lately and threads always are a complicated topic... 如果我错了,请有人纠正我,我最近没睡太久,线程始终是一个复杂的话题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么知道应用程序在不同的国家/地区启动/更新 - How do I know app is launched / updated in a different country Android:如何知道是否可以启动应用 - Android: How to know if an app can be launched 当启动Android设备上的任何应用程序时,该怎么做? - How to do some action when any app on Android device is launched? Android:服务销毁(onDestroy)时如何调用活动? - Android: How do I call an activity when service is destroyed (onDestroy)? Android,当我最小化它销毁的应用程序? - android when i minimize app it destroyed? 我怎么知道那个特定的应用程序。 在android中通过我的应用程序启动 - how can I know that certain app. in android is launched through my Application 我怎么知道该应用程序已在android中完成? - how do i know that app finished in android? 如何知道是否通过Android上的通知启动了应用/游戏? - How to know if app/game was launched via notification on Android? Smooch ConversationActivity,我如何知道活动何时启动? - Smooch ConversationActivity, how can I know when the activity was launched? 如何通过点击推送通知来了解应用程序是否已启动 - how to know if the app was launched by clicking on the push notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM