j l
j l

Reputation: 61

Service keeps crashing before application opens

I am having trouble starting a Service. It crashes before the application even starts.

      Intent repSer = new Intent(this,repService.class);

      startService(repSer);

this is the Service itself.:

public class repService extends Service {
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    while(true){
        Thread timer = new Thread(){
            public void run(){
                try {
                    sleep(5000);
                    // do something

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    // do something
                }
            }
        };
        timer.start();  
    }   
}
}

perhaps this is wrong? my manifest entry:

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

Upvotes: 0

Views: 130

Answers (2)

Vipul
Vipul

Reputation: 28093

If you try to execute this code you will surely get java.lang.OutOfMemoryError Since the loop is never going to break.

That is the main reason your application is crashing.

Also make sure you have defined <service android:name=".repService" /> in manifest

Your manifest should look something like following

enter image description here

Upvotes: 0

Anthony Atkinson
Anthony Atkinson

Reputation: 3248

You're just constantly looping through the while and starting new Threads with timers inside. You're probably overloading with Threads. As fast as the while() statement can loop you're creating another Thread and starting it.

Upvotes: 1

Related Questions