在崩溃后的And​​r​​oid应用程序会自动重新启动 [英] Android app restarts automatically after a crash

查看:111
本文介绍了在崩溃后的And​​r​​oid应用程序会自动重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是用C / C ++的部分写在本机应用程序。的问题是,无论何时C / C ++部分崩溃由于某种原因该应用管芯,然后自动重新启动。这会导致各种混乱的问题。

My app is partly written in native app using C/C++. The problem is that whenever C/C++ part crashes for some reason the app dies and then restarts automatically. This causes all kinds of messy problems

现在,当然,它的应该不会崩溃在本机的一部分,我想剔除所有原因,为什么它会发生。但是,如果它确实发生了,我想:

Now of course, it should not crash in the native part and I'm trying to weed out all reasons why it would happen. However, if it does happen I'd like to:

  1. 退出摆好
  2. 如果它死了,至少不会尝试自动重新启动。

我很好奇,为什么这种行为发生。某些搜索后,我试图把以下行的Andr​​oidManifest.xml中的主要活性元素:

I'm curious as to why this behaviour happens. After some search I tried putting the following line in the main activity element of the AndroidManifest.xml:

android:finishOnTaskLaunch="true"

但自动恢复仍然发生。

but the automatic restore still happens.

任何人都知道为什么发生这种情况,如何改变呢?

Anyone knows why this is happening and how to change it?

更新: 我认为,一个更根本的问题是,
 有没有类似的回调的东西,如果有一个本地崩溃?

UPDATE: I think a more fundamental question is,
Is there something similar to a callback if there is a native crash?

答案之一提出处理崩溃的信号。我会很感激它如何能在应用程序或模块级别进行任何联系。

One of the answers suggested 'handling crash signals'. I'd be grateful for any links on how it can be done at an application or module level.

由于目前为,如果有一个崩溃的应用程序就这样消失,没有什么在logcat中,因此无需调试是可能的。

As it stands currently, if there is a crash the app just disappears, there's nothing in logcat, so no debugging is possible.

推荐答案

试图处理碰撞信号(SIGSEGV等),并发送杀死自己的信号处理程序。这一招可以帮助我。

Try to handle crash signals (SIGSEGV etc.) and send kill to yourself in signal handler. This trick helps me.

例如:

#include <signal.h>
#include <unistd.h>


static void signal_handler(int signal, siginfo_t *info, void *reserved)
{
  kill(getpid(),SIGKILL);
}

extern "C" jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
  struct sigaction handler;
  memset(&handler, 0, sizeof(handler));
  handler.sa_sigaction = signal_handler;
  handler.sa_flags = SA_SIGINFO;
  sigaction(SIGILL, &handler, NULL);
  sigaction(SIGABRT, &handler, NULL);
  sigaction(SIGBUS, &handler, NULL);
  sigaction(SIGFPE, &handler, NULL);
  sigaction(SIGSEGV, &handler, NULL);
  sigaction(SIGSTKFLT, &handler, NULL);
  return(JNI_VERSION_1_6);
}

UPDATE2

如果你想看到的崩溃日志的android logcat中,你应该使用这个信号处理程序

if you want to see crashlog in android logcat you should use this signal handler

static void signal_handler(int signal, siginfo_t *info, void *reserved)
{
 struct sockaddr_un addr;
 size_t namelen;
 socklen_t alen;
 int s, err;
 char name[] = "android:debuggerd";
 namelen  = strlen(name);

 // Test with length +1 for the *initial* '\0'.
 if ((namelen + 1) > sizeof(addr.sun_path)) {
    errno = EINVAL;
    return;
 }

 /* This is used for abstract socket namespace, we need
  * an initial '\0' at the start of the Unix socket path.
  *
  * Note: The path in this case is *not* supposed to be
  * '\0'-terminated. ("man 7 unix" for the gory details.)
  */
 memset (&addr, 0, sizeof addr);
 addr.sun_family = AF_LOCAL;
 addr.sun_path[0] = 0;
 memcpy(addr.sun_path + 1, name, namelen);

 alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;

 s = socket(AF_LOCAL, SOCK_STREAM, 0);
 if(s < 0) return;

 RETRY_ON_EINTR(err,connect(s, (struct sockaddr *) &addr, alen));
 if (err < 0) {
    close(s);
    s = -1;
 }

 pid_t tid = gettid();
 if(s>=0)
 {
   /* debugger knows our pid from the credentials on the
    * local socket but we need to tell it our tid.  It
    * is paranoid and will verify that we are giving a tid
    * that's actually in our process
    */
    int  ret;

    RETRY_ON_EINTR(ret, write(s, &tid, sizeof(unsigned)));
    if (ret == sizeof(unsigned)) {
        /* if the write failed, there is no point to read on
         * the file descriptor. */
        RETRY_ON_EINTR(ret, read(s, &tid, 1));
        //notify_gdb_of_libraries();
    }
    close(s);
 }

 wait(NULL);
 kill(getpid(),SIGKILL);
}

我从Android源把它(不能插入链接,因为android.git.kernel.org关闭),但我不知道它会在未来的Andr​​oid版本

I took it from android source (can't insert link because android.git.kernel.org is down), but I am not sure that it will work in future Android releases

这篇关于在崩溃后的And​​r​​oid应用程序会自动重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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