mercredi 27 mai 2015

onPostExecute() does not send the intent to the onNewIntent() in the backgroud

I am facing problem with onPostExecute() in the background so I am sending data like long, lat, mac to the server every 30 seconds and a response is coming back with the result as:

{
    "status":230,
    "routes":[9, 11],
    "distance":293.3018920430205
}

which are being processed well when the App runs in the front (the data is being passed to the onNewIntent method) but when it runs in the background the data is not being passed to onNewIntent in the MainActivity at all. How can I get that to work to also send the route and the distance to the MainActivity when the app is running in the background?

public class PostData {
    String jSONString;


    // Context mContext;

    public PostData() {
        super();

    }

    public String getjSONString() {
        return jSONString;

    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    public void post_data(String jSONString, Context context) {
        this.jSONString = jSONString;

        new MyAsyncTask(context).execute(jSONString);
        //new MyAsyncTask(context).cancel(true);


    }

    class MyAsyncTask extends AsyncTask<String, Integer, Void> {
        final Context mContext;
        ArrayList<Integer> routes = new ArrayList<Integer>();
        double distance;

        public MyAsyncTask(Context context) {
            mContext = context;
        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            BufferedReader reader = null;

            try {
                System.out.println("The output of : doInBackground "
                        + params[0]);

                URL myUrl = new URL(
                        "http://ift.tt/1Ew06A3");
                HttpURLConnection conn = (HttpURLConnection) myUrl
                        .openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                conn.setRequestProperty("Content-Type", "application/json");
                conn.connect();

                // System.out.println("The output of getResponsecode: "
                // + conn.getResponseCode());
                // create data output stream
                DataOutputStream wr = new DataOutputStream(
                        conn.getOutputStream());
                wr.writeBytes(params[0]);

                wr.close();

                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(
                        conn.getInputStream()));
                String line;

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");

                }

                Gson gson = new Gson();
                StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);

                routes = data.getRoutes();
                distance = data.getDistance();

                System.out.println("The output of the StringBulder: "
                        + sb.toString());

            } catch (IOException e) {

                e.printStackTrace();
                return null;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                        return null;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {

            // Intent with Conetxt of the Asyntask class and
            if (routes != null && !routes.isEmpty())  {
                // if(routes !=null ){
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.putIntegerArrayListExtra("stop_route", routes);
                 intent.putExtra("stop_distance", distance);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                mContext.startActivity(intent);

            } else {
                Log.e("123", "Avoiding null pointer, the routes are null!!!");

            }

        }

    }

}

Aucun commentaire:

Enregistrer un commentaire