Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android服务进程在数小时后被终止_Android_Service_Process - Fatal编程技术网

Android服务进程在数小时后被终止

Android服务进程在数小时后被终止,android,service,process,Android,Service,Process,目前我正在创建一个程序,它需要一个应该始终运行的服务。它工作得很完美,但几个小时后服务就不再执行了。该服务中有一个计时器,它将收集所有可用的WiFi网络并将其发送到服务器 在该服务不再工作并且我导航到Android中正在执行的应用程序后,我看到我的应用程序带有“0进程和1服务”,例如,Facebook上说“1进程和1服务”。这意味着进程将被终止,因此服务将不再运行。我想问你的问题是如何防止这种情况,或者我如何解决这种情况?我的活动绑定到服务器,并在“onStartCommand”上返回“STAR

目前我正在创建一个程序,它需要一个应该始终运行的服务。它工作得很完美,但几个小时后服务就不再执行了。该服务中有一个计时器,它将收集所有可用的WiFi网络并将其发送到服务器

在该服务不再工作并且我导航到Android中正在执行的应用程序后,我看到我的应用程序带有“0进程和1服务”,例如,Facebook上说“1进程和1服务”。这意味着进程将被终止,因此服务将不再运行。我想问你的问题是如何防止这种情况,或者我如何解决这种情况?我的活动绑定到服务器,并在“onStartCommand”上返回“START\u STICKY”

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

编辑 我不想让它成为前台服务,在服务停止时重新启动服务对我来说没问题。前台服务意味着一个通知栏,这不是我想要的

编辑2

我现在使用alarmmanager来检查它是否正在运行:

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);
        }

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

编辑编号3


“编辑2”似乎不起作用。尚未找到解决方案。

要使您的服务永远持续下去,您需要将其设置为前台服务。 点击这个链接,
我希望这能对您有所帮助。

谢谢您的回复,这可能是一个解决方案,但前台服务不是我想要的,因为这意味着我必须显示通知,而这不是我想要的。您好,您找到这个问题的答案了吗?我的应用程序目前也存在同样的问题。Laetan,我应用的解决方案是每隔x小时使用alarmmanager启动/重新启动服务。好的,谢谢您的帮助: