在Android服务中每天一次在特定时间安排TimerTask [英] Schedule a TimerTask at specific time once per day in a service in android

查看:170
本文介绍了在Android服务中每天一次在特定时间安排TimerTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TimerTask对象 timerTask .我的代码正在运行,但是当服务最初启动时,我的计时器任务会在指定时间即下午3:30之前立即运行,尽管我希望它每天仅在下午3:30运行一次.

I have a TimerTask object timerTask. My code is running but when service gets started initially, my timer task runs instantly before the specified time i.e. 3:30 PM, though I want it to run on 3:30 PM once per day only.

Calendar cal = Calendar.getInstance();
cal.set(Calendar.AM_PM, Calendar.PM);
cal.set(Calendar.HOUR, 3);    
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, 0);
Date date = cal.getTime();
timer.schedule(timerTask, date, 1000*60*60*24); // once per day

推荐答案

其他社区用户建议不要使用Timertask,

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

.

  1. 向广播接收器注册警报管理器,在那里重写onReceive方法(触发alamr时的操作)
  2. 将警报设置为每天的时间间隔和要执行的时间.


以以下代码段为例:


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();
}
}


如果可以在App中根据用户输入动态更改时间,则只需更改Calender firingCal变量即可调整时间.


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

这篇关于在Android服务中每天一次在特定时间安排TimerTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆