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" />
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.
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.
You should call stopSelf () to stop a service. After you call it, the Android Framework will call onDestroy() method automatically.
intent service always runs on the worker thread triggered from the main thread.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With