前台的服务就会被杀死,每次 [英] Foreground service gets killed every time

查看:420
本文介绍了前台的服务就会被杀死,每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始我的服务,拉链很多,它总是被打死在后台文件后约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:反应信号3
dalvikvm:写的堆栈跟踪到/data/anr/traces.txt


解决方案

我不使用的问题解决了服务,但 IntentService 。然后,我把整个的压缩和解code到 IntentService 并调用 onHandleIntent 方法> 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.

这篇关于前台的服务就会被杀死,每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆