下班后Android服务进程被杀死 [英] Android service process being killed after hours

查看:67
本文介绍了下班后Android服务进程被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在创建一个需要始终运行的服务的程序.它运行良好,但几个小时后服务不再执行.在服务中有一个计时器,它将收集所有可用的 WiFi 网络并将其发送到服务器.

Currently I am creating a program which requires a service which should always run. It works perfect but after a few hours the service is not executing any more. In the service there is a timer which will collect all the available WiFi networks and send it to a server.

在服务不再工作并且我导航到正在 Android 中执行的应用程序后,我看到我的应用程序带有0 process and 1 service"(Facebook,例如,说1 个进程和 1 个服务").这意味着进程正在被终止,因此服务不再运行.我对你的问题是如何防止这种情况或我如何解决这个问题?我的活动绑定到服务器,并在onStartCommand"上返回START_STICKY".

After the service is not working any more and I navigate to apps that are being executed in Android I see my app with '0 process and 1 service' (Facebook, for example, says '1 process and 1 service'). That means the process is being killed so the service is not running anymore. My question to you is how to prevent this or how an I solve this? My activity is binded to the server and on the 'onStartCommand' I return 'START_STICKY'.

我还阅读了一些关于 AlarmManager 的内容,但如果我理解得很好,它将每 x 秒启动一次服务.我想要的是一个服务,在我说出来之前不会停止运行,或者在进程被 Android 系统终止后重新启动.

I also read something about AlarmManager but if I understand it well this will start the service every x seconds. What I want is a service that not stops with running until I say it or that's being restarted after the process is being killed by the Android system.

编辑我不想让它成为前台服务,一种在服务停止时重新启动服务的方法对我来说很好.前台服务就是通知栏,这不是我想要的.

Edit I don't want to make it a foreground service, a way to restart my service when it's being stopped is fine for me. A foreground service means a notification bar, that's not what I want.

编辑 2

我现在正在使用一个警报管理器来检查它是否正在运行:

I am now doing it with a alarmmanager that checks if it's running or not:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Intent broadcast_intent = new Intent(this, RunChecker.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  broadcast_intent, 0);

    Calendar cal = Calendar.getInstance();
    if (cal.get(Calendar.SECOND) >= 1)
        cal.add(Calendar.MINUTE, 1);
    cal.set(Calendar.SECOND, 1);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 300*1000, pendingIntent);

还有我的接收器:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

     ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
         List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
     boolean isFind = false;
        for (RunningServiceInfo runningServiceInfo : services) {
            //Log.v("StrollSwap", runningServiceInfo.service.getClassName());
            if (runningServiceInfo.service.getClassName().equals("com.example.strollswap.StrollSwapService")){
                isFind = true;
            }
        }
        if(!isFind)
        {
            Intent startServiceIntent = new Intent(context, StrollSwapService.class);
            context.startService(startServiceIntent);
        }

}

我知道代码看起来很糟糕,但它用于测试,如果它按我想要的方式工作,我会重写它:).如果有其他解决方案或解决方案肯定有效,请分享.

I know the code looks bad but it's for testing and I will rewrite it if it's working the way I want :). If there are other solutions or solutions that will definitely works please share.

编辑数字 3

似乎编辑 2 不起作用..还没有找到解决方案.

It seems like edit 2 is not working..no solution found yet.

推荐答案

为了让您的服务永远持续下去,您需要将其设为前台"服务.点击此链接,在前台运行服务我希望这会帮助你.

To keep your service ongoing forever you need to make it a "Foreground" service. Follow this link , Running a Service in the Foreground I hope this will help you.

这篇关于下班后Android服务进程被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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