当应用程序在Samsung S3上运行时,后台进程随机死亡

kol*_*ild 9 android samsung-mobile

我写了一个Android应用程序,除了三星S3之外,我试过的每部手机都没有问题.会发生什么,当我的应用程序运行时,其他后台进程(其他应用程序,即)将开始死亡.例如,在使用我的应用程序一段时间后,动态壁纸将会死亡,用户将在退出我的应用程序后在手机上获得黑色背景.最后,经过一段时间的使用后,我的应用程序也被操作系统杀死了.令人沮丧的是,我在日志中看不到任何错误消息,这会给我一个关于究竟是什么问题的提示.

这仅在三星S3上发生(例如,不在S2上).

我认为它与内存有关,因为我的应用程序非常耗费内存(它正在从Web加载大量图像),但我在日志中看不到任何"无内存"错误.

我还怀疑我使用的HTTP框架可能是错误的,所以我从Apache HttpComponents切换到java.net.HttpURLConnection,但它没有帮助.

任何关于为什么会发生这种情况的想法或对如何调试它的提示都将不胜感激.

以下是日志的摘录,其中显示了一些死亡的背景过程(例如,动态壁纸):

01-07 01:57:37.245: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
01-07 01:57:37.505: D/dalvikvm(29490): GC_FOR_ALLOC freed 1535K, 17% free 28032K/33735K, paused 71ms, total 71ms
01-07 01:57:37.580: D/dalvikvm(6718): WAIT_FOR_CONCURRENT_GC blocked 0ms
01-07 01:57:37.620: D/dalvikvm(6718): GC_EXPLICIT freed 91K, 9% free 17663K/19399K, paused 3ms+5ms, total 40ms
01-07 01:57:38.190: I/InputReader(2296): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.428 ]
01-07 01:57:38.190: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:38.190: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:38.190: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:38.190: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:38.190: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:38.335: I/InputReader(2296): Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=]
01-07 01:57:38.335: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
01-07 01:57:38.335: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
01-07 01:57:38.335: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
01-07 01:57:38.750: D/dalvikvm(29490): GC_FOR_ALLOC freed 1688K, 18% free 27981K/33735K, paused 114ms, total 114ms
01-07 01:57:39.695: W/PowerManagerService(2296): Timer 0x3->0x3|0x0
01-07 01:57:39.700: D/PowerManagerService(2296): setTimeoutLocked::SmartSleep : after19500
01-07 01:57:39.930: I/InputReader(2296): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.429 ]
01-07 01:57:39.930: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:39.930: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:39.930: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:39.930: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:39.930: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x0
01-07 01:57:40.020: D/DeviceInfo(2296): SysScope Service has unexpectedly disconnected!
01-07 01:57:40.065: I/InputReader(2296): Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=]
01-07 01:57:40.065: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
01-07 01:57:40.065: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
01-07 01:57:40.065: I/InputDispatcher(2296): Delivering touch to current input target: action: 0x1
**01-07 01:57:40.080: I/ActivityManager(2296): Process com.android.smspush (pid 28031) (adj 1) has died.**
01-07 01:57:40.090: W/WallpaperService(2296): Wallpaper service gone: ComponentInfo{com.sec.ccl.csp.app.secretwallpaper.themetwo/com.sec.ccl.csp.app.secretwallpaper.themetwo.SecretWallpaperService}
01-07 01:57:40.125: D/KeyguardViewMediator(2296): setHidden false
**01-07 01:57:40.135: I/ActivityManager(2296): Process com.android.server.device.enterprise:remote (pid 28016) (adj 1) has died.**
**01-07 01:57:40.145: I/ActivityManager(2296): Process com.sec.ccl.csp.app.secretwallpaper.themetwo (pid 29715) (adj 1) has died.**
01-07 01:57:40.285: D/dalvikvm(29490): GC_CONCURRENT freed 1616K, 17% free 28289K/33735K, paused 27ms+21ms, total 178ms
01-07 01:57:40.285: D/dalvikvm(29490): WAIT_FOR_CONCURRENT_GC blocked 112ms
01-07 01:57:40.445: D/dalvikvm(2296): GC_CONCURRENT freed 1869K, 59% free 24186K/57991K, paused 26ms+12ms, total 197ms
01-07 01:57:40.660: D/dalvikvm(29490): GC_CONCURRENT freed 1587K, 16% free 28622K/33735K, paused 3ms+7ms, total 51ms
01-07 01:57:40.685: D/KeyguardViewMediator(2296): setHidden false
Run Code Online (Sandbox Code Playgroud)

Lui*_*uis 9

后台进程可以停止的主要原因有三个,我将从较少开始到最可能的开始:

你问无意中要求它停下来

检查代码中是否存在可能导致服务停止的错误.这可能在您的应用程序的服务或其他活动中.

未捕获的异常

该服务遇到了未被捕获的异常.在这种情况下,您应该能够在日志中看到信息,但是日志不会持续很长时间,您可能会错过它.

要解决这种可能性,您可以设置DefaultUncaughtExceptionHandler如下:

UncaughtExceptionHandler currentHandler = Thread.getDefaultUncaughtExceptionHandler();
if (!(currentHandler instanceof DefaultExceptionHandler)) {
    // Register default exceptions handler
    Thread.setDefaultUncaughtExceptionHandler(
            new DefaultExceptionHandler(currentHandler));
}
Run Code Online (Sandbox Code Playgroud)

并定义您的DefaultExceptionHandler类以写入临时文件,如下所示:

public class DefaultExceptionHandler implements UncaughtExceptionHandler {
    private UncaughtExceptionHandler defaultExceptionHandler;

    public DefaultExceptionHandler(UncaughtExceptionHandler pDefaultExceptionHandler)
    {
        defaultExceptionHandler = pDefaultExceptionHandler;
    }

    public void uncaughtException(Thread t, Throwable e) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        e.printStackTrace(printWriter);
        try {
            String filename = "your_temp_filename";
            BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
            bw.write(result.toString());
        bw.flush();
        bw.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        defaultExceptionHandler.uncaughtException(t, e);        
    }
}
Run Code Online (Sandbox Code Playgroud)

Android操作系统要求服务停止

根据您的描述,这是最可能的原因,也是您对它的控制较少的原因.

在需要aditional资源或者认为您的服务运行时间过长时,无法阻止Android杀死您的应用程序.服务在被杀之前运行所允许的时间可能会因设备而异,因为它取决于分配的资源(即图像内存需求随屏幕大小而变化)和可用资源.

在这种情况下,您可以做的唯一合理的事情就是开始foreground service使用以下服务:

    startForeground(int id, Notification notification);
Run Code Online (Sandbox Code Playgroud)

通知操作系统您的服务具有用户可见性,并且将被置于不太可能杀死服务的列表中.我在一些设备上使用它的经验是,即使您同时使用其他几个应用程序,服务也会不间断地运行.

问候.


kol*_*ild 7

我已经找到了这个问题.它实际上是由此处描述的Typeface.createFromAsset()方法引起的内存泄漏.泄漏的内存是本机内存(不在Java堆上),这就是没有OutOfMemory异常的原因.

奇怪的是,这个问题应该从Honeycomb(3.0)开始修复,但就我而言,它发生在运行JellyBean(4.1)的S3上.(我试过几个S3,他们都有这个问题).如果能够访问运行JellyBean的不同手机的人进行测试会很有趣,所以我们可以看看它是否在那里重现(我发布的链接显示了一种简单的方法来重现它).