Tabellen leeren vom Database im Server.

  • 0 Antworten
  • Letztes Antwortdatum
T

the_time

Ambitioniertes Mitglied
0
Hallo,

ich versuche die Tabellen im Server zu leeren wenn die app geschlossen wird. Ich versuche es von der onDestroy() aber onHandleIntent() wird nicht aufgerufen. Wie kann ich das in diesem Fall machen? Hat jd einen Vorschlag?

Also wenn ich den Code in onDestroy() in onPause probiere, wird die IntentService class gerufen. Diese line "System.out.println("ABC MainActivity onDestroy().");" wird in onDestroy() ausgegeben.

Ich will nicht die Tabellen entleeren wenn die App in den Background geht oder wenn ich die activity wechsele, darum kann ich es nicht in onPause() oder onStop() machen.

MainActivity class


Code:
@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);
      System.out.println("ABC MainActivity onDestroy().");
          }

IntentService class:


Code:
  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:

Code:
   <service android:name=".ClearTable" />
 
Zuletzt bearbeitet:
Zurück
Oben Unten