MrPencil
MrPencil

Reputation: 964

invoke the IntentService from onDestroy

I want to empty the table on the server side for a mac address when the user closes the application. Therefor, I am starting the IntentService class from the onDestroy() method in the MainActivity but the Service is not being started. I have already registered it in the Manifest. When I put the code in onDestroy in onStop() the Service starts.

I cant do this job in onPause() or onStop since the app must be able to record data (latitude, longitude, Speed, mac and time) in the Background.

If it is not possible in this way How can I manage it?

onDestroy in the MainActivity:

@Override
protected void onDestroy() {
    super.onDestroy();
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    String macAddress = wInfo.getMacAddress();

     JsonObject  jsonObject = new  JsonObject();
     jsonObject.addProperty("mac", macAddress);

    System.out.println("JsonObject" + jsonObject);

    String json = jsonObject.toString();

    Intent intent2 = new Intent(MainActivity.this,
            ClearTable.class);
    intent2.putExtra("json_mac", json);
    startService(intent2);

}

IntentService class:

public class ClearTable extends IntentService{

    public ClearTable() {
        super("IntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        BufferedReader reader = null;

        try {
            String jSONString = intent.getStringExtra("json_mac");
            System.out.println("xyz The output of : doInBackground "
                    + jSONString);
            URL myUrl = new URL(
                    "https://serverside-apple.rhcloud.com/webapi/test");
            HttpURLConnection conn = (HttpURLConnection) myUrl
                    .openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.connect();             
            DataOutputStream wr = new DataOutputStream(
                    conn.getOutputStream());
            // write to the output stream from the string
            wr.writeBytes(jSONString);
            wr.close();             
            System.out.println("xyz The output of getResponsecode: "
             + conn.getResponseCode());

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            if (reader != null) {
                try {
                    reader.close();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }   

    }

}

Manifest:

 <service android:name=".ClearTable" />

Upvotes: 6

Views: 87

Answers (1)

Sharp Edge
Sharp Edge

Reputation: 4192

Instead of trying your luck with onDestroy() , which is very unreliable as its not guaranteed to be called you should decide something else:

As you stated in your question:

I want to empty the table on the server side for a mac address when the user closes the application.

So why don't you override the onBackPressed() method of the base Activity(Launching Activity) ? and handle your logic in their.

EDIT

After OP's comment about doing something, when user closes the app in background: No!! There is no reliable way to check/detect when your app gets killed in the background either user forcefully closes or System decides to kill it, in case it needs to recover memory.

Upvotes: 4

Related Questions