0

I am facing problem that is driving me crazy. I hope someone can explain me this strange behaviour of my intent. So I want to pass JSON string {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 10:16:49","route":4} to my Service class "PostData" but I am getting null as well as the JSON string in the onStartCommand() method at the same time where the formatted value is always a old one and not of the current time which I am passing to the class.

The calling part in the inner class of the MainActivity:

String jSONString = convertToJSON(pLong, pLat, formatted);
Intent intentJson = new Intent(MainActivity.this, PostData.class);
intentJson.putExtra("json_data", jSONString);
startService(intentJson);

The PostData class:

public class PostData extends IntentService {

    public PostData() {
        super("someone");
        // TODO Auto-generated constructor stub
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        String jSONString = intent.getStringExtra("json_data");
    if(jSONString != null){

        System.out.println("Output from onStartCommand "+jSONString);
    }

    }

}

I tried to debugg it too but always when the debuger is in onStartCommand I am getting null as output!!

This screenshot was captured when the debugger was at this line 'startService(intentJson);' : enter image description here

This is some of the output I am getting in onStartCommand method. Here formatted has always the same timeStamp: 23.04.2015 18:37:49 In the debugging mod I am always getting null as output!!

04-24 11:29:22.785: I/System.out(23721): Output from onStartCommand {"latitude":53.86898202,"longitude":10.66561591,"formatted":"24.04.2015 11:29:23","route":1}
04-24 11:29:22.805: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:23.766: I/System.out(23721): Output from onStartCommand {"latitude":53.86898202,"longitude":10.66561589,"formatted":"24.04.2015 11:29:24","route":1}
04-24 11:29:23.806: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:24.787: I/System.out(23721): Output from onStartCommand {"latitude":53.868982,"longitude":10.66561591,"formatted":"24.04.2015 11:29:25","route":1}
04-24 11:29:24.807: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:25.758: I/System.out(23721): Output from onStartCommand {"latitude":53.868982,"longitude":10.66561591,"formatted":"24.04.2015 11:29:26","route":1}
04-24 11:29:25.818: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:26.809: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
Mr Asker
  • 2,300
  • 11
  • 31
  • 56
  • why at all do you override onStartCommand? i dont say it will fix your problem but all you need is to override onHandleIntent – pskink Apr 24 '15 at 08:54
  • @pskink: I have already moved the code to the onHandleIntent() method but I am now getting one with the current time `24.04.2015 11:21:36` and one with the old one `23.04.2015 18:37:49"` – Mr Asker Apr 24 '15 at 09:25
  • so you startService once but onHandleIntent is called twice? strrrrrrrrrange.... – pskink Apr 24 '15 at 09:26

2 Answers2

1

The error must be somewhere else. Intent and bundle works correctly. You can relay on that!

Do you start multiple times the PostData Service? May this cause confusion, since intent service works like a queue? It handles one intent after each other and starts with the new intent when the previous work is done.

Additionally, you should handle the intents data in onHandleIntent and not in onStartCommand. There is no need to override onStartCommand. Probably this will cause an error, since I'm not sure which Intent is passed here as parameter if the IntentService is already running and takes a new Intent from the queue.

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • Yes I started it multiple time I am sticking at it since yeserday morning. I want to send the data to the server every 60 seconds. How can I clean the queue o get in the last one? – Mr Asker Apr 24 '15 at 08:52
  • I don't think that this is the problem. Your App start the intent service every 60 seconds with passing the current data. That should be fine. Does it really make any diffrence if the data is uploaded after 80 seconds to your server? Hower, I don't think that clearing the queue is supported. However you could copy the source code of Intent Service and implement that functionality as described here: http://stackoverflow.com/questions/7318666/android-intentservice-how-abort-or-skip-a-task-in-the-handleintent-queue – sockeqwe Apr 24 '15 at 09:02
  • But that is not your problem! Try to fire just one Intent to start your service (not every 60 seconds) and debug it. Im pretty sure that there is a problem where you set the data in the intent that starts the service. – sockeqwe Apr 24 '15 at 09:03
  • It is bus tracker. I need the time to know when the data has been recorded since if the there is no internet connection the data should be stroed in the internal storage for short time (maybe 10 minutes) until the connection is avialable if not then delete it. I am getting now output from the current time `11:11:08` and one with the old time `18:37:49` – Mr Asker Apr 24 '15 at 09:13
  • I think the problem is that you override `onStartCommand()`. Just move your code from `onStartCommand` into `onHandleIntent()`. I'm not sure which intent is passed as argument to `onStartCommand` if the intent comes from IntentService internal queue. – sockeqwe Apr 24 '15 at 09:15
  • ok, and you are sure that `jSONString` is that what you expect when starting the intent service `intentJson.putExtra("json_data", jSONString);` Add a breakpoint at this line and check it again and put a second breakpoint in the IntentService onHandleIntent() and compare the values. If they don't match, do you override the extra `json_data` somewhere? – sockeqwe Apr 24 '15 at 09:29
0

You have to set the values in intent and pass that intent while starting the service, and you can get the values in serivce onStartCommand(Intent intent, int flags, int startId).

This is how you have to pass that integer through intent.

private class StartClick implements View.OnClickListener {      
        public void onClick(View v) {
            Intent intent = new Intent(main_activity,ServiceMP3.class);
            intent.putExtras("position",4);
            main_activity.startService(intent);

        }
    }

You can get the values is service like this

public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
   int position = intent.getIntExtra("position", 0);
   }
}
Rajan Bhavsar
  • 1,977
  • 11
  • 25