【问题标题】:How to schedule at particular time service in android如何在android中的特定时间安排服务
【发布时间】:2015-07-15 16:28:09
【问题描述】:

我正在制作演示 android 项目,我想在其中安排每天 8:00 的服务。我从我的启动器活动中调用此方法。每当我启动应用程序时,就会调用下面的方法,然后同时启动服务并显示通知。我只希望它应该以这样的方式安排,它应该在 8:00 执行,而不是每次我打开应用程序时执行。

public static void scheduleVerseNotificationService(Context mContext) {
    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(mContext, Notification.class);
    PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);

    // Set the alarm to start at approximately 08:00 morning.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

}

谢谢

【问题讨论】:

    标签: android android-service alarmmanager android-pendingintent


    【解决方案1】:

    您似乎为已经过去的时间安排了计时器,所以一旦您请求alarmManager.setInexactRepeating,它就会被调用

    这是解决问题的代码:

    public static void scheduleVerseNotificationService(Context mContext) {
        AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(mContext, Notification.class);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);
    
        // reset previous pending intent
        alarmManager.cancel(pendingIntent);
    
        // Set the alarm to start at approximately 08:00 morning.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
    
        // if the scheduler date is passed, move scheduler time to tomorrow
        if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
    
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);
    }
    

    【讨论】:

      【解决方案2】:

      我在理解您的问题时遇到了一些困难,所以:

       I only want it should schedule in such a way it should execute at 8:00 instead of everytime I open app.
      

      我理解为“我只想在 8:00 执行一个 Activity,而不是在我开始那个 Activity 时” 所以,一个建议是:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Intent YOUR_INTENT = getIntent();
      if (YOUR_INTENT!= null) {
       if(YOUR_INTENT.getStringExtra("YOUR_PACKAGE.A_VAR_NAME") != null){
        if (YOUR_INTENT.getStringExtra("YOUR_PACKAGE.A_VAR_NAME").equals("A VALUE")) {
         DO_THE_CODE_YOU_WANT();
        }
      }
      

      然后,根据您的意图,添加一个值:intent.putExtra("YOUR_PACKAGE.A_VAR_NAME", "A VALUE");

      【讨论】:

        【解决方案3】:

        尝试将此行用于pendingIntent

        pendingIntent= PendingIntent.getBroadcast(mContext, 0, intent, 0);
        

        【讨论】:

          猜你喜欢
          • 2012-08-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-30
          • 1970-01-01
          • 2020-12-01
          • 2020-09-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多