因此,我有一个应用程序,需要在工作时定期将数据包发送到服务器,并且它要工作大约2周才能完成特定任务。这个应用程式大量使用BLE和WiFi进行通讯。当移动设备进入睡眠模式并在发送数据包时速度变慢时,会发生此问题。我需要一种使服务负责这项工作以及其中运行的线程活着并保持通信速度的方法。

最佳答案

你应该使用一项服务

创建服务:

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



其他解决方案

在某些设备中,有一个选项可以添加特定的应用程序到列表中,并防止在设备处于睡眠状态时终止该列表,您也可以尝试执行此操作

10-05 21:18
查看更多