Android 从onDestroy调用IntentService

Android 从onDestroy调用IntentService,android,Android,我想在用户关闭应用程序时清空服务器端的表以获得mac地址。因此,我正在从MainActivity中的onDestroy()方法启动IntentService类,但服务没有启动。我已经在舱单上登记了。当我将代码放入onDestroy中的onStop()中时,服务启动 我无法在onPause()或onStop中完成此项工作,因为应用程序必须能够在后台记录数据(纬度、经度、速度、mac和时间) 如果不能以这种方式实现,我如何管理它 主活动中的onDestroy: @Override protected

我想在用户关闭应用程序时清空服务器端的表以获得mac地址。因此,我正在从MainActivity中的
onDestroy()
方法启动IntentService类,但服务没有启动。我已经在舱单上登记了。当我将代码放入onDestroy中的
onStop()
中时,服务启动

我无法在
onPause()
onStop
中完成此项工作,因为应用程序必须能够在后台记录数据
(纬度、经度、速度、mac和时间)

如果不能以这种方式实现,我如何管理它

主活动中的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);
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" />
}

意向服务类别:

@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);
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" />
清单:

@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);
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" />

不要用
onDestroy()
试试运气,这是非常不可靠的,因为它不能保证会被调用。你应该决定其他事情:

正如你在问题中所说:

我想在用户关闭应用程序时清空服务器端的表以获得mac地址

那么,为什么不重写基本活动(启动活动)的
onBackPressed()
方法呢?在他们的工作中处理好你的逻辑

编辑

在OP关于做某事的评论之后,当用户在后台关闭应用程序时:
不没有可靠的方法来检查/检测您的应用程序在后台被杀死的情况,或者用户强制关闭,或者系统决定杀死它,以防它需要恢复内存。

onDestroy()
不保证被调用,因此,将重要的代码放入其中是危险的。那么,在我的案例中,我还能将其放在哪里呢?您能接受我的回答吗?只是一个想法:您还可以安排在服务器上及时进行表清理。依靠客户端通知服务器它不再存在似乎是一种糟糕的设计,因为它会造成内存泄漏:任何恶意第三方都可能产生多个客户端来填充服务器的客户端映射。如果第三方随后突然终止客户端应用程序,你的地图中有很多死物,这些死物永远不会被删除,因为你计划只在通过客户端调用通知它们缺乏相关性时删除它们。因为在这种情况下,当用户在后台关闭应用程序时,我想删除服务器端的记录。当用户在后台关闭应用程序时。。嗯,没有办法处理这种情况。。在这件事上相信我。。我自己浪费了太多的时间做这样的事情还有
onBackPressed()的问题
用户无需按下后退按钮即可关闭应用程序,或者用户不会在一个活动中按下后退按钮,因为上一个活动是启动活动alsp。在这种情况下,按下该按钮没有任何意义。您的启动活动只应显示一次,而在按下后退时不会再次显示。。就像我说的,在你的场景中没有可靠的方法。。大多数人使用
onPause()
方法进行清洁,但如果设备处于锁定状态或屏幕变黑且应用程序仍在运行,则会调用
onPause()
。。