简体   繁体   English

如何检查onCreate是否已杀死同一活动的先前android进程?

[英]how to check in onCreate that the previous android process of the same activity has been killed?

I have an activity in whose onCreate method an Init function is called (the function calls some native code involving lot of stuffs and calls to the openSLES audio api). 我有一个活动,在该活动的onCreate方法中调用了一个Init函数(该函数调用一些涉及很多内容的本机代码,并调用openSLES音频api)。 The point is that this Init function makes the app crash when called again, which happens on a screen rotation or when i close the activity using Back button and i launch it again (but if in the meanwhile the process is killed, i have no troubles). 关键是,此Init函数会使应用程序在再次调用时崩溃,发生在屏幕旋转或当我使用``后退''按钮关闭活动并再次启动它时发生的情况(但是与此同时,如果该进程被终止,则我没有任何麻烦)。 I can't change the beaviour of the Init function. 我无法更改Init函数的行为。

I see that the process isn't killed when the activity is destroyed, I expected this after reading the docs, and it's a good thing since - if there is some audio signal playing - that continues playing after the activity has been destroyed, which is good for my purposes. 我看到活动被销毁后进程并未终止,我希望在阅读文档后能够做到这一点,这是一件好事,因为-如果正在播放一些音频信号-活动被销毁后仍会继续播放,这是对我的目的很好。

I tried to perform a check on the initialization state using onSaveInstanceState, but that works well only on screen-rotation, that's when onSaveInstanceState is called. 我尝试使用onSaveInstanceState对初始化状态进行检查,但仅在屏幕旋转时(即调用onSaveInstanceState时)才能很好地工作。 The callback is not called when i push the Back button. 当我按下“后退”按钮时,不会调用该回调。

So i tried to use Shared Preferences, performing the state saving in onPause. 所以我尝试使用共享首选项,在onPause中执行状态保存。 But at this point i have the opposite problem: if the process is killed, the Shared Preferences values are kept, but in that case i need to perform Init again for the app to work properly. 但是在这一点上,我遇到了相反的问题:如果进程被终止,则共享首选项值会保留,但是在那种情况下,我需要再次执行Init才能使应用正常运行。

I guess i need a way to know for sure if my activity is created after a process kill or not, but at the moment i can't see how. 我想我需要一种方法来确定我的活动是否在进程终止后创建,但是目前我看不到。 I thought about using the bundle instance in onPause method, but i can't figure how and whether this is possible. 我考虑过在onPause方法中使用bundle实例,但是我不知道如何以及是否可行。 Any kind of hint would be really appreciated. 任何提示将不胜感激。

  1. You can store pid of your process in shared preferences. 您可以将进程的pid存储在共享首选项中。 If you compare in YourActivity.onCreate your current pid with stored one, you can determine when you must initialize OpenSLES. 如果在YourActivity.on中进行比较,请使用存储的ID创建当前pid,则可以确定何时必须初始化OpenSLES。
  2. You can initialize OpenSLES in Application-derived class, in YourApplication.onCreate - it's called only once. 您可以在源自Application的类的YourApplication.onCreate中初始化OpenSLES-仅调用一次。

edit: 编辑:

Ie declare following class: 即声明以下类:

public class YourApplication extends Application {
  static private native synchronized void InitOpenSLES();

  public YourApplication() {}

  // see http://developer.android.com/reference/android/app/Application.html#onCreate() for details
  @Override
  public void onCreate() {
    super.onCreate();
    InitOpenSLES();
  }
}

For each and every process you have pid or process id. 对于每个进程,您都有pid或进程ID。 In your init function you can easily get the thread id and can save it in any integer value. 在您的init函数中,您可以轻松获取线程ID并将其保存为任何整数值。

Thread.currentThread().getId())); Thread.currentThread()。getId()));

whenever your activity will restart just check that thread id is same or different. 每当您的活动重新开始时,只需检查线程ID相同或不同即可。 If thread id is different then call your function init function. 如果线程ID不同,则调用函数init函数。 Otherwise you have already done. 否则,您已经完成了。

There's a simple solution to this problem. 这个问题有一个简单的解决方案。 You don't need to save things in SharedPreferences to accomplish this. 您无需在SharedPreferences中保存内容即可完成此操作。 Just use a static (class) variable. 只需使用静态(类)变量。 Like this: 像这样:

public class Globals {
    public static boolean initialized = false;
}

The variable initialized will be set to false when the class is loaded. 加载类时, initialized的变量将设置为false。 Only once. 只有一次。 In your code, you then check and set the variable like this: 在代码中,然后检查并设置变量,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Initialize (but only once per process)
    if (!Globals.initialized) {
        init(); // Call init function that does things one time per process
        Globals.initialized = true; // Remember we are initialized so we don't
                                    //  do it again
    }
    ...
}

Even if all your activities are finished, if the OS doesn't kill your process the variable initialized will still be "true" if the application is started again. 即使您的所有活动都已完成,但如果操作系统没有终止您的进程,则如果再次启动该应用程序,则initialized的变量仍将为“ true”。 Once the OS kills the process, the variable will be set to "false" the next time the application is started and a new process is created. 一旦OS终止了进程,则下次启动应用程序并创建新进程时,变量将设置为“ false”。

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

相关问题 应用程序进程被杀死后片段onCreate - Fragments onCreate after app process been killed Android 在系统杀死应用程序后没有重新打开以前的活动 - Android is not reopening previous activity after system has killed app 我的Android APP进程已被终止,但仍在RunningTask中 - My Android APP's process has been killed, but still in RunningTask Android-在进程被杀死时恢复状态 - Android - restoring state when process has been killed 如果该活动被Android运行时杀死,该如何转到上一个活动? - How to go to previous activity if the activity is killed by Android runtime? Android:如何在BroadCast Receiver中检查一次活动是否已完成 - Android: how to check in BroadCast Receiver that activity has been completed once Android:如何避免 Activity 被系统杀死,ActivityManager:处理我的 package 名称(pid 935)已死亡 - Android:How to Avoid Activity being killed by System,ActivityManager: Process my package name (pid 935) has died 如果App进程终止,捆绑包如何发送到onCreate? - How is bundle sent to onCreate if App process killed? 如何知道一个进程(app)是否被杀死? - how to know whether a process(app) has been killed? 一旦我的应用程序进程被终止,我的活动状态是什么? - What is holding my activity's state once my application process has been killed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM