是否可以在手机关机前发送 HttpRequest?

发布于 2024-10-30 18:48:15 字数 813 浏览 5 评论 0原文

我想在手机关闭之前向某个服务器发送请求。为此,我将实现一个接收 ACTION_SHUTDOWN 广播的 BroadcastReceiver。收到此广播后,服务将向某个服务器发出 HttpRequest。

由于手机已经关闭,我可能无法验证服务器是否收到请求,但这并不重要。我担心的是广播到关闭之间的窗口是否足以让服务发送请求。

我浏览了互联网,发现了这个:

#!/system/bin/sh
stop;
stop dhcpcd;

sleep 1;

for i in `cat /proc/mounts | cut -f 2 -d " "`;
do
   busybox 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

这里。文章指出,这是Android关机脚本。正如我们所看到的,dhcpcd 是第一个在关闭时停止的守护进程。但我仍然不知道脚本执行之前是否有足够的时间。

那么,是否可以在手机关机前发送HttpRequest呢?如果是这样,怎么办?

编辑:假设存在网络连接,并且手机通过正常方式关闭。

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
   busybox 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-11-06 18:48:15

简单地尝试一下。另一种方法是简单地将关机时所需的信息保存在首选项文件中(例如关机时间,...)。然后在手机启动时将此数据发送到您的服务器(并且您可以检查服务器是否确实收到它并在不成功时重试)。

您甚至可以想象将两者都做为收到 ACTION_SHUTDOWN 后发送数据表明有时间发送一些网络数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文