Mohamed Elkamaly
Mohamed Elkamaly

Reputation: 2568

Android - Timer inside service

I have timer inside Android service I need this timer to be started once, and I have multi call to this service. Each time I call startService() the service starts new timer (the timer execute its job as much as the startService() is been called).

This is the code

    private TimerTask notificationTask = new TimerTask() {
    @Override
    public void run() {
        posNotification();
    }
};

    @Override 
public void onCreate() {
    super.onCreate();
    notificationTimer.schedule(notificationTask, 1000L, 10 * 1000L);
}

Upvotes: 1

Views: 2797

Answers (1)

sUndeep
sUndeep

Reputation: 360

You can keep a value at shared preference or take a global variable. Set it to 0 initially. When service get called, check the value. If it is 0 start the timer and set the global value to 1. So next time, it will not start your timer. Does it make sense to you? Kindly look at a sample code I prepared and let me know if it resolves your problem:

   package com.example.timerinservice;
   
   import android.app.Activity;
   import android.content.Context;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;
   
   public class MainActivity extends Activity {
       
       Button btnStartService;
       Context context;
       int service_count = 0;
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           
           context = MainActivity.this;
           btnStartService = (Button) findViewById(R.id.buttonStartService);
           
           btnStartService.setOnClickListener(new OnClickListener() {
               
               @Override
               public void onClick(View arg0) {
                   Intent intent = new Intent(context, TimerService.class);
                   intent.putExtra("servicecount", service_count);
                   context.startService(intent);
                   service_count++;
               }
           });
       }
   
   }
   
    package com.example.timerinservice;
    
    public class GlobalVariable {
        public static boolean TIMER_STYARTED = false;
    }
    package com.example.timerinservice;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.IntentService;
    import android.content.Intent;
    import android.util.Log;
    
    public class TimerService extends IntentService {
        
        Timer notificationTimer;
        int get_service_count = 0;
    
        public TimerService() {
            super("timerservice");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            get_service_count = intent.getExtras().getInt("servicecount");
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            if(!GlobalVariable.TIMER_STYARTED){
                notificationTimer = new Timer();
                notificationTimer.schedule(notificationTask, 1000L, 10 * 1000L);
                
                GlobalVariable.TIMER_STYARTED = true;
            }
            
        }
        
        private TimerTask notificationTask = new TimerTask() {
            @Override
            public void run() {
               //posNotification(); 
                Log.d("INSIDE","run() of TimerTask for: "+get_service_count);
            }
        };
    }

Upvotes: 2

Related Questions