2015-08-25 50 views
1

我想在用戶關閉應用程序時清空服務器端的Mac地址表。因此,我正嘗試在ClearTable類中調用emptyTable()。目前,當我關閉應用程序emptyTable()被調用,但'donInBackground()'中沒有發生什麼?是否有可能在調用onDestroy時向服務器發送請求

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); 

    String json = jsonObject.toString(); 

    ClearTable ct = new ClearTable(); 
    ct.emptyTable(json); 
} 

ClearTable類:

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.ArrayList; 

import android.os.AsyncTask; 
import android.os.Handler; 
import android.util.Log; 

import com.google.gson.Gson; 
import com.json.JSONStore; 

public class ClearTable { 

    public void emptyTable(String json){ 
     new MyAsyncTask().execute(json); 

    } 

    class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<Integer>> { 
     ArrayList<Integer> routes = new ArrayList<Integer>(); 
     @Override 
     protected ArrayList<Integer> doInBackground(String... params) { 
      BufferedReader reader = null; 

      try { 
       System.out.println("The output of : doInBackground " 
         + params[0]); 
       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(params[0]); 
       wr.close();    
       System.out.println("xyz The output of getResponsecode: " 
       + conn.getResponseCode()); 

      } catch (IOException e) {  
       e.printStackTrace(); 
       return null; 
      } finally { 
       if (reader != null) { 
        try { 
         reader.close(); 
         return null; 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      return null; 

     } 

     protected void onPostExecute(ArrayList<Integer> result) { 

     } 

    } 

} 

編輯代碼與IntentService:

在MainActivity的onDestroy:

@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類:

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(); 
       } 
      } 
     } 

    } 

} 

清單:

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

在Android documntaion有關的onDestroy: 「注意:不要在這個方法不算被稱爲保存數據的地方。例如,如果一個活動是在內容提供商編輯數據,這些修改應該在致力於要麼onPause()或onSaveInstanceState(Bundle),不在這裏。「 –

回答

0

,你可以在這個地方使用的後臺服務。在onDestroy()中調用該服務。 而這個過程將結束服務。

ex。

  1. http://javatechig.com/android/creating-a-background-service-in-android
  2. https://developer.android.com/training/run-background-service/create-service.html
+0

我可以使用'IntentService'嗎? –

+0

是的,按照我更新的答案 –

+0

請參閱我的編輯代碼。我已經用IntentService嘗試過了,但是在'onDestroy()啓動服務之後''ClearTable'服務沒有被調用!? –

0

使用後臺服務時的onDestroy方法調用是一個不錯的選擇,但如果你退出與android.os.Process.killProcess(android.os.Process您的應用程序。 myPid()),onDestroy可能不會調用。

+0

我沒有使用'android.os.Process.killProcess(android.os.Process.myPid())'也許Android,因爲當我把代碼放在onStrop()的時候就可以工作。 –

相關問題