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

时间:2013-01-07 01:17:04

标签: android samsung-mobile

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

仅在Samsung 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

1 个答案:

答案 0 :(得分:9)

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

你无意中要求它停止

检查您的代码,查找在尴尬的情况下可能要求服务停止的错误。这可能在您的应用程序的服务或其他活动中。

未捕获的异常

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

要解决这种可能性,您可以像这样设置DefaultUncaughtExceptionHandler

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

并定义你的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);        
    }
}

Android操作系统要求服务停止

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

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

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

    startForeground(int id, Notification notification);

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

问候。