user390749
user390749

Reputation:

Android Service Won't Start

Strange problem I'm facing. I want to start a Service when my Activity is created. Here is my Service's code (very simple, as you'll see):

package com.nblmedia.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ServiceSynchro extends Service {

public ServiceSynchro()
{
    Log.i(XXX.LOG, "ServiceSynchro");
}

@Override
public IBinder onBind(Intent intent) {
    Log.i(XXX.LOG, "onBind");
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Log.i(XXX.LOG, "onCreate");
    super.onCreate();
}

@Override
public void onDestroy() {
    Log.i(XXX.LOG, "onDestroy");
    // TODO Auto-generated method stub
    super.onDestroy();
}
}

In my AndroidManifest.xml I have declared the Service as follow :

<service android:name=".ServiceSynchro"></service>

Finaly, to start my Service, I call the startService method in the onCreate :

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    startService(new Intent(UserMenuActivity.this, ServiceSynchro.class));      
}

And nothing happens. If I check the device's running services, there are no service named ServiceSyncho running. If I check the Logcat, nothing is outputted. Same for the breakpoints, the app doesn't cycle thru the Service's lifecycle events. Any idea what could be my problem?

Upvotes: 1

Views: 5450

Answers (3)

guydemossyrock
guydemossyrock

Reputation: 1118

I looked everywhere when my service didnt start. Nothing worked out.Finally debugging of the third hour ı found out that my package name was not the standart format xx.xx.xx package names.I fixed and it worked. For your information.

Upvotes: 0

user390749
user390749

Reputation:

I found the problem. In my AndroidManifest.xml file, the service:name attribute wasn't refering to the fully qualified classname of the service. As example, I was using this :

<service android:name=".ServiceSynchro"></service>

When I needed :

<service android:name="com.xxx.service.ServiceSynchro"></service>

For the people interested in the application from the documentation, here is the link to view it.

android:name The name of the Service subclass that implements the service. This should be a fully qualified class name (such as, "com.example.project.RoomService"). However, as a shorthand, if the first character of the name is a period (for example, ".RoomService"), it is appended to the package name specified in the element.

Lastly, I updated the code to override the onStartCommand command since the onStart method is deprecated.

public int onStartCommand(Intent intent, int flags, int startId)

Upvotes: 10

CommonsWare
CommonsWare

Reputation: 1007494

Get rid of your constructor. Never implement a constructor in a component (sole current exception: IntentService).

Also, when you do implement constructors elsewhere in Java, chain to the superclass.

Upvotes: 4

Related Questions