Schedule a TimerTask at specific time once per day in a service in android

后端 未结 1 732
北荒
北荒 2020-12-11 10:42

I have a TimerTask object timerTask. My code is running but when service gets started initially, my timer task runs instantly before the specif

相关标签:
1条回答
  • 2020-12-11 11:16

    As other community users suggested, don't use Timertask, android has a AlarmManager

    that you can use.

    HOW?

    1. Register the alarm manager with a broadcast receiver, there override the onReceive method (what to do when the alamr is fired)
    2. Set the alarm with the interval daily and the time to be executed.

    Here a Snippet as Example:

    public class MainActivity extends Activity {
    private static final String TAG ="MainActivity";
    PendingIntent myPendingIntent;
    AlarmManager alarmManager;
    BroadcastReceiver myBroadcastReceiver;
    Calendar firingCal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Register AlarmManager Broadcast receive.
        firingCal= Calendar.getInstance();
        firingCal.set(Calendar.HOUR, 8); // At the hour you want to fire the alarm
        firingCal.set(Calendar.MINUTE, 0); // alarm minute 
        firingCal.set(Calendar.SECOND, 0); // and alarm second
        long intendedTime = firingCal.getTimeInMillis();
        
        registerMyAlarmBroadcast();
        alarmManager.set( AlarmManager.RTC_WAKEUP, intendedTime , AlarmManager.INTERVAL_DAY , myPendingIntent );
    }
     
    private void registerMyAlarmBroadcast()
    {
          Log.i(TAG, "Going to register Intent.RegisterAlramBroadcast");
    
        //This is the call back function(BroadcastReceiver) which will be call when your 
        //alarm time will reached.
        myBroadcastReceiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                Log.i(TAG,"BroadcastReceiver::OnReceive()");
                Toast.makeText(context, "Your Alarm is there", Toast.LENGTH_LONG).show();
            }
        };
    
        registerReceiver(myBroadcastReceiver, new IntentFilter("com.alarm.example") );
        myPendingIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.alarm.example"),0 );
        alarmManager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    }
    private void UnregisterAlarmBroadcast()
    {
        alarmManager.cancel(myPendingIntent); 
        getBaseContext().unregisterReceiver(myBroadcastReceiver);
    }
    
    @Override
    protected void onDestroy() {
        unregisterReceiver(myBroadcastReceiver);
        super.onDestroy();
    }
    }
    

    Edit:

    if the time can changed dynamically in the App depending on user-inputs, then just change the Calender firingCal variable for adjusting the time.

    0 讨论(0)
提交回复
热议问题