Android 应用程序在崩溃后自动重启 [英] Android app restarts automatically after a crash

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

问题描述

我的应用部分是使用 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. 如果死机,至少不要尝试自动重启.

我很好奇为什么会发生这种行为.经过一番搜索后,我尝试将以下行放在 AndroidManifest.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"

但自动恢复仍然发生.

任何人都知道为什么会发生这种情况以及如何改变它?

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 等)并在信号处理程序中将 kill 发送给自己.这个技巧对我有帮助.

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);
}

更新2

如果你想在 android logcat 中查看 crashlog,你应该使用这个信号处理程序

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* ''.
 if ((namelen + 1) > sizeof(addr.sun_path)) {
    errno = EINVAL;
    return;
 }

 /* This is used for abstract socket namespace, we need
  * an initial '' at the start of the Unix socket path.
  *
  * Note: The path in this case is *not* supposed to be
  * ''-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 已关闭),但我不确定它是否会在未来的 Android 版本中工作

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

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

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