2015-08-25 51 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()的时候就可以工作。 –

相关问题