Why is this simple service not starting?

AndroidService

Android Problem Overview


I have a service with a handler that has to write "Hello" in the logcat every 5 seconds. But it doesn't write nothing on the logcat... It's like the service is not executing, and I put a breakpoint on it and the debug mode never stops on the breakpoint.

I start the service, in the first activity of my app, with this:

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

I am sure that the code startService is executed because it is called before starting another activity, and the other activity starts.

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");
	}    
}

Android Solutions


Solution 1 - Android

Did you declare the service in AndroidManifest.xml?

Solution 2 - Android

Very important: write the name space correctly, for example:

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

in my AndroidManifest.xml previously it was (wrong):

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

No service started and no error message!

Solution 3 - Android

Hi the code you write is working fine. May be you forgot to add the following code in the manifest file before closing application tag.

<application>
    ....
    <service android:name=".MyServiceNotifications"/>
</application>

Solution 4 - Android

There are also circumstances where you need to put the "enabled" attribute to "true" when defining it in the manifest, like so:

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

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

Solution 5 - Android

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's no need to put it in the AndroidManifest.xml.

Solution 6 - Android

Very rare case scenario. But when wroking on multiprocess application the activity and the service need to have android:process mentioned in the Manifest.xml

EG: RemoteServiceBindingActivity.kt

startService(Intent(this, RemoteService::class.java))

Then

<activity
    android:name=".RemoteServiceBindingActivity"
    android:process=":test" />


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

Didn't work. I had to add android:process=":test" to the service too in the Manifest like this:

<service
    android:name=".RemoteService"
    android:enabled="true"
    android:process=":test"/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNullPointerExceptionView Question on Stackoverflow
Solution 1 - AndroidFrancoView Answer on Stackoverflow
Solution 2 - AndroidSeraphim'sView Answer on Stackoverflow
Solution 3 - AndroidVenkaReddyView Answer on Stackoverflow
Solution 4 - AndroiddbmView Answer on Stackoverflow
Solution 5 - AndroidnoelicusView Answer on Stackoverflow
Solution 6 - AndroidSaran SankaranView Answer on Stackoverflow