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 sticky后台服务在应用程序启动后重新启动';关门了_Android_Android 4.2 Jelly Bean_Sticky_Background Service - Fatal编程技术网

Android sticky后台服务在应用程序启动后重新启动';关门了

Android sticky后台服务在应用程序启动后重新启动';关门了,android,android-4.2-jelly-bean,sticky,background-service,Android,Android 4.2 Jelly Bean,Sticky,Background Service,我正在开发一个Android应用程序,需要每隔一个小时左右在后台进行一些更新。我有一个后台服务,这是我做的。我正在使用Timer.scheduleAtFixedRate来安排更新 这似乎很有效。但我注意到,当我关闭应用程序时,下次运行计划更新时,它会导致再次调用Application.onCreate 这是一个问题,因为Application.onCreate是我从API中抓取数据以准备显示给用户的地方。我不希望这发生在幕后 这是预期的行为吗?如果是这样,也许我需要在创建时添加一个签入,以查看应

我正在开发一个Android应用程序,需要每隔一个小时左右在后台进行一些更新。我有一个后台服务,这是我做的。我正在使用Timer.scheduleAtFixedRate来安排更新

这似乎很有效。但我注意到,当我关闭应用程序时,下次运行计划更新时,它会导致再次调用Application.onCreate

这是一个问题,因为Application.onCreate是我从API中抓取数据以准备显示给用户的地方。我不希望这发生在幕后

这是预期的行为吗?如果是这样,也许我需要在创建时添加一个签入,以查看应用程序是否首先位于前台?或者我搞错了什么

谢谢

p、 这是一款运行Jelly Bean 4.2.1的Galaxy三星

后台服务代码:

@EService
public class BackgroundService extends Service {

    ...

    private Timer timer = new Timer();

    private void performUpdate() {

        // Do the stuff here that we need to do on a schedule...
        Log.i(LOG_CONTEXT, "Perform scheduled update");

        ...            

    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d(LOG_CONTEXT, "Background thread started");

        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                performUpdate();
            }
        }, 0, UPDATE_INTERVAL);

        // Sticky means service will continue running until explicitly stopped
        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        Log.d(LOG_CONTEXT, "Background thread stopped");            
        timer.cancel();

    }

}
申请代码:

@EApplication
public class MyApplication extends Application {

    ...

    @Override
    public void onCreate() {
        super.onCreate();
        initApp();
    }

    private void initApp() {

        // This is where I want to do stuff when the app is actually
        // opened by the user, not every time the background service
        // update occurs!

        Log.i(LOG_CONTEXT, "Initialise. Why does this happen again after app's closed?");

        ...

    }

    ...
日志:


您的服务作为应用程序的一部分运行,因此将为其创建应用程序


大多数应用不需要扩展
应用程序
。在没有看到所有代码的情况下,我很确定您也不需要这样做。只需为向用户显示内容的类扩展
Activity
,并在其中执行API内容。服务运行时不会创建此警报。

这不是问题的原因,但您确实应该使用警报,而不是让服务不断运行。同意。为这个角色使用
计时器
是不可靠的,而且用户对它怀有敌意(因为它在每个事件之间都会毫无理由地占用一堆RAM)。使用
AlarmManager
IntentService
代替。谢谢Ralgha和Commonware。那我来看看AlarmManager。我之前忽略了它,因为我认为它只是用于需要在特定时间发生的事情,而我只想每小时更新一次,但具体时间并不重要。但如果效率更高,那么听起来这是一个不错的选择。干杯啊,这是有道理的。然后我将把代码转移到活动中。谢谢
12-09 16:28:15.828: I/MyApplication(3049): Initialise. Why does this happen again after app's closed?

[Now I close the app, by pressing the Recent Apps menu button and swiping it away]

12-09 16:28:16.015: I/BackgroundService(3049): Perform scheduled update
12-09 16:28:33.875: I/MyApplication(3080): Initialise. Why does this happen again after app's closed?