Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invoke the IntentService from onDestroy

Tags:

android

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" />
like image 458
MrPencil Avatar asked Aug 25 '15 12:08

MrPencil


People also ask

How do I start IntentService?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.

How do I use IntentService?

To use it, extend IntentService and implement onHandleIntent(android. content. Intent) . IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

Does stopSelf call onDestroy?

You should call stopSelf () to stop a service. After you call it, the Android Framework will call onDestroy() method automatically.

Does intent Service run on main thread?

intent service always runs on the worker thread triggered from the main thread.


1 Answers

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.

like image 141
Sharp Edge Avatar answered Oct 19 '22 20:10

Sharp Edge