android - 如何在手机休眠时保持网络通讯畅通

标签 android network-programming wifi bluetooth-lowenergy

所以我有一个应用程序需要在工作时定期向服务器发送数据包,它需要工作大约 2 周才能完成某项任务。此应用大量使用 BLE 和 WiFi 进行通信。当移动设备进入休眠模式并在发送数据包时速度变慢时,就会出现问题。我需要一种方法来使负责此作业的服务和其中运行的线程保持 Activity 状态并保持通信速度。

最佳答案

你应该使用服务

创建服务:

public class YourService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int id) {
        // do your jobs here
        return super.onStartCommand(intent, flags, id);
        }
    }

创建一个 Application 类并启动您的服务:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, YourService.class));
    }
}

查看更多信息here


其他解决方案

在某些设备中,有一个选项可以将特定的应用程序添加到列表中,并防止在设备休眠时终止该列表,您也可以试试这个

关于android - 如何在手机休眠时保持网络通讯畅通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003754/

相关文章:

Android Canvas.drawText

越过电线

python - 如何创建 python 套接字监听器守护进程

java - NetworkChangeReceiver的onReceive方法在3G和WIFI同时开启时被多次调用

安卓 : set visibility of individual buttons in a custom ListView

java - ListViewAdapter extends CursorAdapter 滚动时顺序困惑

android - Android 中的安全功能

c++ - 如何创建/设计数据结构?

ip - tcpdump 和 wifi 重传

iphone - 如何在iPhone上获取Wifi的RSSI?