Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service needs to run always (Never pause or stop)

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.

like image 207
Ashekur Rahman Molla Asik Avatar asked Apr 02 '13 07:04

Ashekur Rahman Molla Asik


People also ask

How do you stop a service from stopping on Android?

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.

How does Android service always run in the background using Kotlin?

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.


2 Answers

"Is it possible to run this service always as when the application pause and anything else?"

Yes.

  1. In the service onStartCommand method return START_STICKY.

    public int onStartCommand(Intent intent, int flags, int startId) {         return START_STICKY; } 
  2. 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); 
  3. Create the binder.

    public class MyBinder extends Binder {         public MyService getService() {                 return MyService.this;         } } 
  4. 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;         } }; 
  5. Bind to the service using bindService.

            Intent intent = new Intent(this, MyService.class);         bindService(intent, m_serviceConnection, BIND_AUTO_CREATE); 
  6. 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()); } 
  7. You need to modify the manifest to launch the activity in single top mode.

              android:launchMode="singleTop" 
  8. 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()); 
like image 182
Stephen Donecker Avatar answered Oct 12 '22 00:10

Stephen Donecker


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

like image 24
Juan Avatar answered Oct 12 '22 00:10

Juan