Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,@R_411_10586@l 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,@R_411_10586@l 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,@R_411_10586@l 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,@R_411_10586@l 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,@R_411_10586@l 197R_651_11845@s
01-07 01:57:40.660: D/dalvikvm(29490): GC_CONCURRENT freed 1587K,16% free 28622K/33735K,paused 3ms+7ms,@R_411_10586@l 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);

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

问候.

大佬总结

以上是大佬教程为你收集整理的android – 在三星S3上运行应用程序时,后台进程随机死机全部内容,希望文章能够帮你解决android – 在三星S3上运行应用程序时,后台进程随机死机所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。