So I have an app that needs to send packets to a server regularly while working and it is meant to work for about 2 weeks to accomplish a certain task. This app heavily uses both BLE and WiFi for communication purposes. The problem occurs when mobile device goes to sleep mode and slows down when sending packets. I need a way to keep the service responsible for this job and threads running in it alive and preserve communication speed.
you should use a service
create a service :
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);
}
}
Create an Application class and start your service:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(this, YourService.class));
}
}
see more information here
Other solution
in some devices there is an option to add a specific app to list and prevent terminate that list while device is sleeping , you can try this too
Firstly, what i'm saying here might be different for every version of Android, but i will say what to do for my version of Android.
Go and turn off this setting: Turn off networking devices while phone is off.
OR: Change this setting: When to sleep?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.