Ammar
Ammar

Reputation: 1233

How to add a timer to a service?

I tried adding a timer to OnCreate method, Onstart, and even to an AsyncTask, but the program would crash. Not too sure why! Here is a portion of my code, and in this case I added to OnStart.

@Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");





    }
    private void startTimer()
    {           

        if (found != "Yes Found")
         {


            TimerTask updateProfile = new CustomTimerTask(MyService.this);
            timer.scheduleAtFixedRate(updateProfile, 0, 60*1000);

         }

    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        //spool.release();

    }

    @Override
    public void onStart(Intent intent, int startid) {

        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");

        startTimer();


    }

Timer Code

public class CustomTimerTask extends TimerTask {


            private Context context;
            private Handler mHandler = new Handler();

            // Write Custom Constructor to pass Context
            public CustomTimerTask(Context con) {
                this.context = con;

            }

            @Override
            public void run() {
                // TODO Auto-generated method stub


                  //some code here


            }




            }

Error Message

03-17 13:16:35.661: E/AndroidRuntime(7634): FATAL EXCEPTION: main
03-17 13:16:35.661: E/AndroidRuntime(7634): java.lang.RuntimeException: Unable to start service com.example.saber.MyService@415c0510 with Intent { cmp=xxxx }: java.lang.NullPointerException
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2387)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.app.ActivityThread.access$1900(ActivityThread.java:127)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.os.Looper.loop(Looper.java:137)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.app.ActivityThread.main(ActivityThread.java:4511)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at java.lang.reflect.Method.invoke(Method.java:511)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at dalvik.system.NativeStart.main(Native Method)
03-17 13:16:35.661: E/AndroidRuntime(7634): Caused by: java.lang.NullPointerException
03-17 13:16:35.661: E/AndroidRuntime(7634):     at com.example.saber.MyService.startTimer(MyService.java:67)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at com.example.saber.MyService.onStart(MyService.java:86)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.app.Service.onStartCommand(Service.java:438)
03-17 13:16:35.661: E/AndroidRuntime(7634):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2370)
03-17 13:16:35.661: E/AndroidRuntime(7634):     ... 10 more

Where exactly am I suppose to add the timer ?

Upvotes: 0

Views: 175

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006664

You need to learn what a NullPointerException is. It means that you are trying to call a method on some object that is null.

The stack trace that you listed shows that your NullPointerException is on line number 67 of MyService.java, in the startTimer() method of MyService.

When you count lines in your code, I suspect that you will find that line number 67 of MyService.java is this line:

timer.scheduleAtFixedRate(updateProfile, 0, 60*1000);

If that is indeed the line that is causing your exception, it would mean that you are getting a NullPointerException because timer is null. Since you have not shown any code where you initialize the timer data member, this is certainly possible.

If line number 67 is not the line that I cite above, then you will need to determine what on line 67 could possibly be null -- note that it will be an object that you are calling a method on in that line.

Also, as others have pointed out, you need to learn how to compare objects in Java. != is comparing object identity, not object equality.

Upvotes: 1

Related Questions