According to your code I see that you link a service so what you are doing is that your Activity interacts with the service:
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
In this case, you could use a Foreground Service ( Foreground Service ) , which is done using the startForeground () :
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
The option I do regularly through a BroadcastReceiver is the following, you can take this article as an example:
link
1) register the service when you start your device, with this you do not have to open the application to start:
<receiver android:name="ReceiverCall" >
<intent-filter>
<action android:name="com.stackoveflow.myservicio" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
2) Add in class BroadcastReceiver
code to restart the service again.
public class ReceiverCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Servicio", "El servicio se detuvo");
context.startService(new Intent(context, ServiceTest.class));;
}
}
3) Create the Android Service class and in onDestroy()
implement the service start.
public void onDestroy() {
Intent intent = new Intent("com.stackoveflow.myservicio");
intent.putExtra("yourvalue", "torestore");
sendBroadcast(intent);
}