【问题标题】:Android service periodically performing tasks in background?Android服务定期在后台执行任务?
【发布时间】:2015-06-25 10:40:48
【问题描述】:

我创建了一个 Android 服务,我只是在我的活动中启动它

Intent i = new Intent();
i.setClassName("com.abc.app", MyService.class.getName());
startService(i);
bindService(i, connection, Context.BIND_AUTO_CREATE);

然后我使用 onServiceConnected 回调来获取我的服务实例,所以我可以调用它的函数

public void onServiceConnected(ComponentName name, IBinder boundService) {
            myService = IMyInterface.Stub.asInterface((IBinder) boundService);
        }

我可以成功调用函数并且可以得到结果。但是我希望我的服务在后台定期执行任务(例如每隔几分钟调用一次的函数)我无法从我的活动中调用函数,因为我在启动服务后完成()我的活动并且我想要我的服务只需在后台运行并执行任务。

【问题讨论】:

  • 你没有使用重复模式的警报服务

标签: java android android-activity android-service android-background


【解决方案1】:

你需要添加一个 TimerTask,看看怎么做here

【讨论】:

    猜你喜欢
    • 2012-01-09
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多