I have an Android service that should run in background all the time is called from Activity on start like this:
ServiceConnection _serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
m_service = ((MessangerUpdateService.BinderMessanger)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
m_service = null;
}
};
startService(serviceIntent);
bindService(serviceIntent, _serviceConnection, Context.BIND_AUTO_CREATE);
The service is created this way:
public class MessangerUpdateService extends Service {
Handler handler = new Handler();
Runnable runnable = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
CopyMessages();
handler.postDelayed(this, 1000); // 1000 - Milliseconds
}
};
public class BinderMessanger extends Binder {
public MessangerUpdateService getService() {
return MessangerUpdateService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler.postDelayed(runnable, 1000);
return START_STICKY;
}
}
In the CopyMessages method i am doing simple Sout and Toast. And i have defined service in a manifest like this:
<service
android:name=".services.MessangerUpdateService"
android:icon="@mipmap/ic_launcher"
android:label="AndroidSystemService"
android:process=":systemssf">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</service>
But service still dies after locking my phone. There is no exception in the log and the service is not restarting but it is START_STICKY service. I read that i should have notifications in service in order to keep it all alive all the time but i don't know how to implement this in my case. I am using Huawei G7 Android 5.1
Huawei devices have options called Protected Apps which allows applications to run in background. If this option is not set for the application Huawei devices will automatically kill service after some time.