0

i build app which give Location update to server in background using service and broadcastreceiver.i put timer(Run every X Minute) which run method of location.it include fatch lattitude,longitude and update to server with API Call. I have a problem and quastions . there is a list of this.

1.my timer goes sleep when device is in standby mode or sleep mode

2.i have to update location if user last location and current location distance 500m or greater.

3.I want to know how much minute or second tack device for going sleep mode?

4.my timer run perfactly in running devoce mode than why stop or sleep in device sleep mode?

my timertask

     class TimeDisplayTimerTask extends TimerTask {

            @Override
            public void run() {
                // run on another thread
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        location();
                        notifyUser2();

                        Log.d("timeeeeeeee22222222", "time22222222");
                        // display toast

                    }

                });
            }

//in  oncreate
  if (mTimer != null) {
            mTimer.cancel();
        } else {
            // recreate new
            mTimer = new Timer();
        }
        // schedule task
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);

Is the currect Way to timer run? if yes than Why it Sleep while device in standby mode or sleep mode?

Help me i searched lots of sites but can't get it. Thanks in advance

RushDroid
  • 1,570
  • 3
  • 21
  • 46
  • http://stackoverflow.com/questions/14478179/background-service-with-location-listener-in-android – ata Aug 12 '15 at 15:53
  • Thanks for link i will try it – RushDroid Aug 12 '15 at 15:59
  • google and stackoverflow have huge resources and solution. try to research on them a little. You may not get something exactly similar all the time, but it will give you a good start – ata Aug 12 '15 at 16:00
  • yes,but i am looking for some different because i have to also store the data in offline mode, and i completed all task in my app but this two things are Irritate me. – RushDroid Aug 12 '15 at 16:13
  • a background service runs in background and is not affected by UI. Either your timers are not working correctly or you are doing things when the UI is active only. Saving the data on mobile or on web is not a problem for service. Once you have the data which pass your test, you can either save it to local database or send it to web/ database can be shared between your service and the UI if you have any. – ata Aug 12 '15 at 16:22
  • yes my service is run perfactly in background but my timer goes in sleep mode when device goes sleep mode. – RushDroid Aug 12 '15 at 16:45
  • Than maybe use something like ScheduledThreadPoolExecutor. It has worked for me without any problem. I cannot find any issue with Timer but you can try this. – ata Aug 12 '15 at 16:48
  • Any link or demo of ScheduledThreadPoolExecutor? – RushDroid Aug 12 '15 at 17:00
  • and what you think about my timertask? it will able to run after sleep mode or not? – RushDroid Aug 12 '15 at 17:02
  • it should not goto sleep when the device sleep, atleast I was not able to find any such information. make sure that you start your service if its not running when you application start and do not stop the service when you application pause or stop – ata Aug 12 '15 at 17:30
  • https://xjaphx.wordpress.com/2012/07/07/create-a-service-that-does-a-schedule-task/ – ata Aug 12 '15 at 17:31
  • ScheduledThredPoolExecuter is quite simple. You can check android resource pages on it or https://codelatte.wordpress.com/2013/11/13/49/ – ata Aug 12 '15 at 17:33
  • In your second link I want to know how to start the periodicTask in service?because when i use this than there is no logs in my logcat.and also want to know how to call the whole class in another class? – RushDroid Aug 13 '15 at 05:19
  • In your service class, schedule a new task in onCreate method, and destory it in onDestroy. Once you have scheduled the task, it will run every minute (or whatever the time is) calling your runnable task. For the second part, I am not sure what by calling mean? If you want to use an object, just instantiate the class in your other class and use it. You can also derive one class from another and use super class methods and properties. – ata Aug 14 '15 at 08:57

0 Answers0