18

I created a new class called HelloService. I added this to the Android manifest.xml.

public class HelloService extends Service {
    private Timer timer = new Timer();
    private long INTERVAL = 5000;

    public void onCreate() {
        super.onCreate();
        startservice();

    }

    private void startservice() {
        timer.scheduleAtFixedRate( new TimerTask() {
            public void run() {
                Log.d("servy", "This proves that my service works.");
            }
        }, 0, INTERVAL);
    ; }

    private void stopservice() {
        if (timer != null){
            timer.cancel();
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

My other activity calls it like this:

    Intent helloservice = new Intent(this, HelloService.class);
    startService(helloservice);

For some reason, I put a breakpoint in my new HelloService...but it's not even hitting. It's not logging either.

Edit: "Unable to start service Intent { cmp = com.examples.hello/.HelloService }: not found"

What does that mean? ... I created HelloService.java in the same place as everything else...


Solved. I fixed my manifest file. Thanks Nikola Smiljanic

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

to:

   <service android:name="HelloService"></service>
Dori
  • 915
  • 1
  • 12
  • 20
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • What have you added to your manifest file? – Nikola Smiljanić Feb 15 '10 at 09:58
  • 20
    If you have resolved your issue please post an answer and accept it. This will remove it from the unanswered cue which is growing by the day. – Moog Sep 09 '11 at 17:15
  • exactly what i have encountered,it is strange that in the official doc:http://developer.android.com/guide/topics/fundamentals/services.html#Declaring , they use ,with the dot as the prefix – DiveInto Sep 21 '11 at 08:31
  • 2
    @Timex - Just a friendly reminder to read Merlins comment again, it would be nice to get the cue cleaned up a bit – Marmoy Nov 29 '11 at 14:00

5 Answers5

7

Maybe you're not declaring the service in your manifiest. Anyway you're using the wrong class. You have to use AlarmManager for programing events. See that link, it was very useful to me.

Use alarmManager and service to perform schedule notification only during specific time period

Community
  • 1
  • 1
adriannieto
  • 352
  • 2
  • 7
3

A service has a life cycle as any other android application. For this reason it can occur that your service gets killed by the system (see Service documentation). The right way to implement this is using Alarm Manager as discussed in Android service stops.

Community
  • 1
  • 1
Luca DC
  • 91
  • 1
  • 2
1

Will you try this:

helloservice.setComponent(new ComponentName
                 (*hello service package name goes here*, 
                                *hello service canonical name goes here*));
startService(helloservice);
0

Declare the your service in mainfest.xml file of your project.

<services  android:name=".SMSReceiver" android:enabled="true">
          <intent-filter>
                  <action android:name=/>
         </intent-filter>
 </services>
Gaurav
  • 114
  • 3
  • 7
0

You need to implement onStartCommand()

http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent,int,int)

adamame
  • 188
  • 11