Can't get json if app is killed with background service(When phone runs the app normally instead of usb with android studio)

Viewed 164

I am getting a json response from a background service.When i have my app open or minimized everything works well but when i kill the app the background service still works but is not fetching any json response(i see that from my logs).Why is that happening?The toast messages are being displayed normally when app is killed.

Here is my background service code

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();

    handler = new Handler();
    runnable = new Runnable() {
        public void run() {
            createNotificationChannel();
            new getJson().execute("https://gekon.technologypark.cz/api/v1/notification/");
            Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
            handler.postDelayed(runnable, 10000);
        }
    };

    handler.postDelayed(runnable, 15000);
}

@Override
public void onDestroy() {
    /* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
    //handler.removeCallbacks(runnable);
    Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
}

class getJson extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... strings) {

        URL obj = null;

        try {
            obj = new URL("https://gekon.technologypark.cz/api/v1/notification/");
            HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("ApiSecret", LoginInfo.ApiSecret);
            conn.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            StringBuilder sb = new StringBuilder();
            String output;
            while ((output = br.readLine()) != null)
                sb.append(output);

            JSONObject jsonObj = new JSONObject(sb.toString());
            Log.d("test", "response" + jsonObj);

            JSONObject dataObj = jsonObj.getJSONObject("data");
            Log.d("test", "TEST LENGHT" + dataObj.length());

            Iterator keys = dataObj.keys();

            while (keys.hasNext()) {
                // loop to get the dynamic key
                String currentDynamicKey = (String) keys.next();

                // get the value of the dynamic key
                JSONObject data = dataObj.getJSONObject(currentDynamicKey);
                // do something here with the value...
                body = data.getString("body");
                Log.d("test", "TEST ID" + body);

                pushNotification(body);
            }

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("test", "PUSH CATCH" + e);


        }
        return null;
    }
}

UPDATE

I found the problem.It was the ApiSecret that was null so i stored it in sharedPreferences and it was ok UNTIL i removed the usb that i'm testing my app with my android studio.When i remove it,it still has the problem but if i have it connected it works fine...Why is that?

0 Answers
Related