У меня есть приложение для обмена сообщениями, основанное на веб-кодах, которые полагаются на фоновое обслуживание для отправки и получения сообщений. Как только служба запускается, мне нужно, чтобы она выполнялась бесконечно в фоновом режиме, даже когда приложение закрывается, а телефон переходит в спящий режим.Разрешение службы постоянно работать в фоновом режиме
В приложении, служба запускается только как пользователь входит в систему, и в методе OnCreate как startService(new Intent(LoggingIn.this, MessagingService.class));
Как я могу настроить мой сервис для постоянно работать в фоновом режиме?
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
imService = ((MessagingService.IMBinder) service).getService();
if (imService.isUserAuthenticated() == true) {
// Intent i = new Intent(LoggingIn.this, ListOfFriends.class);
Intent i = new Intent(LoggingIn.this, MainActivity.class);
startActivity(i);
LoggingIn.this.finish();
}
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
imService = null;
Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
Toast.LENGTH_SHORT).show();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Start and bind the imService
*/
startService(new Intent(LoggingIn.this, MessagingService.class));
...etc
Служба должна запускаться постоянно, если она ломается, тогда сообщения не будут доставлены, это лучший вариант? – Sauron
Даже в этом случае андроид все равно может его убить. Но вы можете использовать это в сочетании с START_STICKY, и это лучшее, что вы получите. – tachyonflux
Как gmail, whatsapp и другие сообщения в режиме реального времени работают так, что они не недовольны службой и связью клюва? – Sauron