前台的服务就会被杀死,每次

编程入门 行业动态 更新时间:2024-10-18 08:28:22
本文介绍了前台的服务就会被杀死,每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我开始我的服务,拉链很多,它总是被打死在后台文件后约8%,15-30秒。我怎样才能prevent这一点,让我的服务完成其任务?该服务未绑定到任何东西,可能这个问题?

//编辑:我创建了一个前台的服务出来,但它仍然被终止。

公共类PackingService延伸服务{    串基本路径=+ Environment.getExternalStorageDirectory();    字符串源=基本路径+/ data.zip;    文件DIR =新的文件(基本路径+/数据);    @覆盖    公共无效的onCreate(){        super.onCreate();    }    @覆盖    公众诠释onStartCommand(意向意图,诠释标志诠释startId){        NotificationCompat.Builder nBuilder =新NotificationCompat.Builder(本)            .setSmallIcon(R.drawable.ic_launcher)            .setContentTitle(正在压缩)            .setContentText(...);        startForeground(1337,nBuilder.build());        尝试{            ZipFile的zip文件=新的ZipFile(源);            Log.d(ZIP,preparing文件...);            ArrayList的filesToAdd =新的ArrayList();            addDirectory(DIR,filesToAdd);            ZipParameters参数=新ZipParameters();            parameters.setCom pressionMethod(Zip4jConstants.COMP_DEFLATE);            parameters.setCom pressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);            zipFile.setRunInThread(真);            ProgressMonitor监视= zipFile.getProgressMonitor();            Log.d(ZIP,创建压缩......);            zipFile.createZipFile(filesToAdd,参数);            INT百分比= -1;            而(monitor.getState()== ProgressMonitor.STATE_BUSY){                如果(百分之!= monitor.getPercentDone()){                    百分比= monitor.getPercentDone();                    Log.d(进步,百分+%);                }            }        }赶上(抛出:Zip​​Exception E){            e.printStackTrace();        }        stopForeground(真);        返回super.onStartCommand(意向,旗帜,startId);    }    @覆盖    公众的IBinder onBind(意向意图){        返回null;    }    公共无效addDirectory(文件目录,ArrayList的filesToAdd){        如果(dir.exists()){            文件[] =文件dir.listFiles();            的for(int i = 0; I< files.length ++我){                文件fil​​e =文件[I]                如果(file.isDirectory()){                    addDirectory(文件,filesToAdd);                }其他{                    filesToAdd.add(文件);                }            }        }}

日志:

dalvikvm:主题ID = 3:反应信号3dalvikvm:写的堆栈跟踪到/data/anr/traces.txt

解决方案

我不使用的问题解决了服务,但 IntentService 。然后,我把整个的压缩和解code到 IntentService 并调用 startForeground 之前。拉拉链完成后我称之为 stopForeground ,一切完美的作品。

When I start my service that zips a lot of files in background it always gets killed after about 8% and 15-30 seconds. How can I prevent that and let my service finish its task? The service is not bound to anything, might this be the problem?

//EDIT: I created a foreground service out of it but it still gets terminated.

public class PackingService extends Service { String basePath = ""+ Environment.getExternalStorageDirectory(); String source = basePath+"/data.zip"; File dir = new File(basePath+"/data"); @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Zipping") .setContentText("..."); startForeground(1337, nBuilder.build()); try{ ZipFile zipFile = new ZipFile(source); Log.d("ZIP", "Preparing files..."); ArrayList filesToAdd = new ArrayList(); addDirectory(dir, filesToAdd); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA); zipFile.setRunInThread(true); ProgressMonitor monitor = zipFile.getProgressMonitor(); Log.d("ZIP", "Creating zip..."); zipFile.createZipFile(filesToAdd, parameters); int percent = -1; while(monitor.getState() == ProgressMonitor.STATE_BUSY){ if(percent != monitor.getPercentDone()) { percent = monitor.getPercentDone(); Log.d("Progress", percent + "%"); } } } catch (ZipException e) { e.printStackTrace(); } stopForeground(true); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { return null; } public void addDirectory (File dir, ArrayList filesToAdd) { if (dir.exists()) { File[] files = dir.listFiles(); for (int i = 0; i < files.length; ++i) { File file = files[i]; if (file.isDirectory()) { addDirectory(file, filesToAdd); } else { filesToAdd.add(file); } } } }

Logs:

dalvikvm﹕ threadid=3: reacting to signal 3 dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'

解决方案

I solved the problem by not using Service but IntentService. I then put the whole zipping code into the onHandleIntent method of the IntentService and call startForeground before it. After zipping finishes I call stopForeground and everything works perfectly.

更多推荐

前台的服务就会被杀死,每次

本文发布于:2023-11-27 07:57:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:就会   被杀   前台

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!