Why does this simple service not start?

I have a service with a handler that should write "Hello" to logcat every 5 seconds. But it doesn’t write anything to logcat ... It looks like the service is not running, and I put a breakpoint on it, and debug mode never stops at the breakpoint.

I start the service in the first action of my application:

startService(new Intent(GPSLoc.this, MyServiceNotifications.class)); //enciendo el service 

I am sure that the startService code is executed because it is called before the start of another action, and another action begins.

This is the code of my service:

 public class MyServiceNotifications extends Service { boolean serviceStopped; private Handler mHandler; private Runnable updateRunnable = new Runnable() { @Override public void run() { if (serviceStopped == false) { createNotificationIcon(); } queueRunnable(); } }; private void queueRunnable() { // 600000 : cada 10 minutos, comprueba si hay nuevas notificaciones y actualiza la // notification BAR mHandler.postDelayed(updateRunnable, 5000); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { serviceStopped = false; // //////////////////////////////////////MANEJADOR SIMILAR A UN HILO mHandler = new Handler(); queueRunnable(); // ///////////////////////////////////// FIN MANEJADOR } @Override public void onDestroy() { serviceStopped = true; } @Override public void onStart(Intent intent, int startid) { } public void createNotificationIcon() { Log.d("MyServiceNotifications", "Hello"); } } 
+52
android service
Jan 21 2018-11-21T00:
source share
5 answers

Have you announced the service in AndroidManifest.xml ?

+158
Jan 21 2018-11-21T00:
source share
— -

Very important: write the namespace correctly, for example:

 <service android:name="com.example.data.synchronization.SynchronizationService"/> 

in my AndroidManifest.xml earlier this was (wrong):

 <service android:name="com.example.data.SynchronizationService"/> 

Service is not running and error message !

+57
Jul 25 '12 at 11:27
source share

Hi, u write code is working fine. You may forget the following code in the manifest file before closing the application tag.

 <application> .... <service android:name=".MyServiceNotifications"/> </application> 
+13
Jan 21 2018-11-11T00:
source share

There are also circumstances in which the “enabled” attribute must be “true” when defining it in a manifest, for example:

 <service android:enabled="true" android:name=".MyServiceNotifications" /> 

See this link for more information: http://developer.android.com/guide/topics/manifest/service-element.html

+6
Jan 21 2018-11-21T00:
source share

If using Xamarin Droid, the easiest way to do this is to mark the class as a service like this:

 [Service] public class LongRunningTaskService : Service { ... } 

Then there is no need to put it in AndroidManifest.xml.

0
May 10 '16 at 9:23
source share



All Articles