Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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_Background Process - Fatal编程技术网

使用服务发送Android池数据

使用服务发送Android池数据,android,background-process,Android,Background Process,我有一个应用程序,将发送100个数据到一个网络api。现在,我正在使用一个将通过调度器运行的服务。我的问题是,调度程序在数据发送完成之前再次启动。如何暂停计划程序,直到进程完成 public class PollReceiver extends BroadcastReceiver { private static final int PERIOD=100000; @Override public void onReceive(Context ctxt, Intent i) { s

我有一个应用程序,将发送100个数据到一个网络api。现在,我正在使用一个将通过调度器运行的服务。我的问题是,调度程序在数据发送完成之前再次启动。如何暂停计划程序,直到进程完成

 public class PollReceiver extends BroadcastReceiver {
 private static final int PERIOD=100000;
  @Override
  public void onReceive(Context ctxt, Intent i)
  {
scheduleAlarms(ctxt);
  }
  static void scheduleAlarms(Context ctxt)
   {
AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(ctxt, ScheduledService.class);
     PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
     mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime() +     PERIOD, PERIOD, pi);
   }
     }
定时接收机

    public ScheduledService()
    {
    super("ScheduledService");
}

@Override
protected void onHandleIntent(Intent intent) 
     {
    if (isOnline())
            {
        //Process Starts 
    }
}
在发送完成后设置新计划。设置第一个时间表后,下一个时间表由服务本身设置:

public class PollReceiver extends BroadcastReceiver {
    private static final int PERIOD=100000;

    @Override
    public void onReceive(Context ctxt, Intent i)
    {
            scheduleNextAlarm(ctxt);
    }

    static void scheduleNextAlarm(Context c)
    {
        AlarmManager mgr=(AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(c, ScheduledService.class);
        PendingIntent pi=PendingIntent.getService(c, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + PERIOD, pi);
    }

    public static class ScheduledService extends IntentService{

        public ScheduledService() {
            super("scheduled_service");
        }


        @Override
        protected void onHandleIntent(Intent intent) {
            try{
            // do your work
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                scheduleNextAlarm(this);
            }
        }
    }
}

同志,从你的代码中,我看不出你是如何执行这一个hudnread请求的,它只显示了每段时间一个请求

我的建议是在每个时段调用您的服务,并在服务中使用队列进行操作。当您调用服务时,您调用了它的同一个实例,所以在以前的任务完成之前调用它应该不是问题

顺便说几句:

服务不创建线程,因此为了避免您遇到异常 必须自己创建线程 IntenService创建线程和 此外,IntentSrevice handle在队列中一个接一个地显示意图-it 似乎是你需要的 您可以实现可序列化或可打包 对于将执行包装到API并在意图中传递它的类 或者存储在磁盘上
不要提前计划所有日程安排。每次会话完成/失败时,请重新安排时间。与其设置重复,不如在每次服务完成其工作时使用设置。我无法做到这一点。请回答我的问题。我只能发送100个数据中的一个。每次我必须启动服务来发送另一个数据。我的一个疑问是,我是从我的主活动开始流程的。如果我打开主活动,服务将从该活动开始。我也是对的。有相同的问题,在完成流程之前进入finally块。请给我举个例子。如果你能,那就太好了是的,我可以,但请你继续提问-我不明白你想要实现什么,你是如何传递数据的?哪些请求应该在服务中的某个时间段内执行?我正在从我的sqlite数据库检索数据并发送到我的服务器。需要调度程序的是,有时我会有10个数据,有时我会有100个数据,所以基本上问题是,服务是在我等待请求时启动的从服务器回拨。这就是为什么我想暂停调度程序,直到我完成同步。注意:同步后,我将从本地数据库中删除数据,因为S.D.提供的这种任务方法将非常有效。它不会阻止UI,因为IntentService将在另一个线程中完成它的工作。