android – 在三星S3上运行应用程序时,后台进程随机死机

前端之家收集整理的这篇文章主要介绍了android – 在三星S3上运行应用程序时,后台进程随机死机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了一个 Android应用程序,除了三星S3之外,我在每个手机上都没有问题.发生什么事情是,当我的应用程序正在运行时,其他后台进程(其他应用程序)将会开始死亡.例如,在使用我的应用程序一段时间后,Live Wallpaper将会死机,用户退出我的应用程序后会在手机上获得黑色背景.最后,经过一段时间的使用,我的应用程序也被操作系统杀死.令人沮丧的是,我无法在日志中看到任何错误消息,这将给我一个提示究竟是什么问题.

这只发生在三星S3(而不是在S2上).

我认为这是内存相关的,因为我的应用程序非常内存密集(它正在从网络加载大量的图像),但是我看不到日志中没有任何内存错误.

我也怀疑我使用的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

解决方法

背景过程有三个主要原因,我将从少到最可能:

你自问,要求它停下来

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

未捕获的异常

该服务遇到了一个未被捕获的例外.在这种情况下,您应该能够看到日志中的信息,但日志不会持续很久,您可能会丢失它.

为了解决这个可能性,你可以像这样设置一个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在需要附加资源或相信您的服务运行时间过长时杀死您的应用程序.允许服务在被杀死之前运行的时间可能会从设备更改为设备,因为它取决于分配的资源(即,图像内存需求随屏幕大小变化)和可用资源.

在这种情况下,您唯一可以做的事情就是开始使用前台服务进行服务:

startForeground(int id,Notification notification);

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

问候.

原文链接:https://www.f2er.com/android/310400.html

猜你在找的Android相关文章