I created a service and want to run this service always until my phone restarts or force closed. The service should run in background.
Sample code of created service and start services:
Start the service:
Intent service = new Intent(getApplicationContext(), MyService.class); getApplicationContext().startService(service);
The service:
public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO do something useful HFLAG = true; //smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000); return Service.START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { // TODO for communication return IBinder implementation return null; } }
Manifest declaration:
<service android:name=".MyService" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > </service>
Is it possible to run this service always as when the application pauses and anything else. After some time my application goes pause and the services also go pause or stop. So how can I run this service in background and always.
If you really needed Android not to kill your service, your best bet would be to make it a System App, or make your service return START_STICKY in your onStartCommand() method. This way, if your service gets killed, then it will be queued to restart automatically.
This example demonstrates how to run an Android service always in the background using Kotlin. Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
"Is it possible to run this service always as when the application pause and anything else?"
Yes.
In the service onStartCommand method return START_STICKY.
public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }
Start the service in the background using startService(MyService) so that it always stays active regardless of the number of bound clients.
Intent intent = new Intent(this, PowerMeterService.class); startService(intent);
Create the binder.
public class MyBinder extends Binder { public MyService getService() { return MyService.this; } }
Define a service connection.
private ServiceConnection m_serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { m_service = ((MyService.MyBinder)service).getService(); } public void onServiceDisconnected(ComponentName className) { m_service = null; } };
Bind to the service using bindService.
Intent intent = new Intent(this, MyService.class); bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
For your service you may want a notification to launch the appropriate activity once it has been closed.
private void addNotification() { // create the notification Notification.Builder m_notificationBuilder = new Notification.Builder(this) .setContentTitle(getText(R.string.service_name)) .setContentText(getResources().getText(R.string.service_status_monitor)) .setSmallIcon(R.drawable.notification_small_icon); // create the pending intent and add to the notification Intent intent = new Intent(this, MyService.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); m_notificationBuilder.setContentIntent(pendingIntent); // send the notification m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build()); }
You need to modify the manifest to launch the activity in single top mode.
android:launchMode="singleTop"
Note that if the system needs the resources and your service is not very active it may be killed. If this is unacceptable bring the service to the foreground using startForeground.
startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
In order to start a service in its own process, you must specify the following in the xml declaration.
<service android:name="WordService" android:process=":my_process" android:icon="@drawable/icon" android:label="@string/service_name" > </service>
Here you can find a good tutorial that was really useful to me
http://www.vogella.com/articles/AndroidServices/article.html
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With