Is it possible to send an HttpRequest just before the phone shuts down?
I want to send a request to some server just before the phone shuts down. To do this, I will implement a BroadcastReceiver that receives the ACTION_SHUTDOWN broadcast. Upon reception of this broadcast the Service will fire an HttpRequest to some server.
I may not be able to verify if the server got the request since the phone has already shut down, but it doesn't matter. My worry is if the window between broadcast to shutdown is enough for the service to send the request.
I looked around the internets and I found this:
#!/system/bin/sh
stop;
stop dhcpcd;
sleep 1;
for i in `cat /proc/mounts | cut -f 2 -d " "`;
do
busyb开发者_如何学Cox mount -o remount,ro $i 2>&1 > /dev/null;
done
sync;
if [ "$1" = "-r" ];
then
toolbox reboot;
fi
if [ "$1" = "-rr" ];
then
toolbox reboot recovery;
else
reboot -p;
fi
here. The article states that this is the Android shutdown script. As we can see, dhcpcd is the first daemon that will be stopped upon shutdown. But still I don't know if there will be enough time before the script executes.
So, is it possible to send an HttpRequest before the phone shuts down? If so, how?
EDIT: Assuming that a network connection is present, and the phone is turned off via normal means.
Simply try it. Another way to do it could be simply to save the info you need on shutdown in a preference file (like shutdown time, ...). Then on phone startup send this data to your server (and you have the bonus to be able to check that the server actually receives it and retry if unsuccessful).
You can even imagine to do both as Send data after ACTION_SHUTDOWN is received suggests that there is time to send out some network data.
精彩评论