【问题标题】:Android execute a function after 1 hourAndroid 1小时后执行一个函数
【发布时间】:2014-08-19 05:05:27
【问题描述】:

我有一个 android 应用程序,当他/她激活该应用程序时,我将用户的数据存储在数据库中。我的应用程序要求用户手动停止应用程序,以便从数据库中删除其条目,以及在激活应用程序时继续运行的其他服务。

所以我想编写一个函数,该函数将在每个小时后执行(当应用程序被激活时)并向用户发出通知,只是为了提醒他/她有关正在运行的服务。如果用户忘记了停止服务,然后他们可以停止它或继续服务。

什么是最有效的方法。我不想在 1 小时的基础上检查用户是否认为它可以运行一天左右的时间消耗太多的电池。请指教。谢谢:)

【问题讨论】:

    标签: android performance timer android-asynctask


    【解决方案1】:

    试试这个方法,希望能帮助你解决问题。

    new Timer().scheduleAtFixedRate(task, after, interval);
    

    【讨论】:

      【解决方案2】:

      我建议代码是这样的。

      // the scheduler
      protected FunctionEveryHour scheduler;
      
      
      // method to schedule your actions
      private void scheduleEveryOneHour(){
      
              PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
                                                                       new Intent(WAKE_UP_AFTER_ONE_HOUR), 
                                                                       PendingIntent.FLAG_UPDATE_CURRENT);
      
              // wake up time every 1 hour
              Calendar wakeUpTime = Calendar.getInstance();
              wakeUpTime.add(Calendar.SECOND, 60 * 60);
      
              AlarmManager aMgr = (AlarmManager) getSystemService(ALARM_SERVICE);        
              aMgr.set(AlarmManager.RTC_WAKEUP,  
                       wakeUpTime.getTimeInMillis(),                 
                       pendingIntent);
      }
      
      //put this in the creation of service or if service is running long operations put this in onStartCommand
      
      scheduler = new FunctionEveryHour();
      registerReceiver(scheduler , new IntentFilter(WAKE_UP_AFTER_ONE_HOUR));
      
      // broadcastreceiver to handle your work
      class FunctionEveryHour extends BroadcastReceiver{
              @Override
              public void onReceive(Context context, Intent intent) {
                      // if phone is lock use PowerManager to acquire lock
      
                      // your code to handle operations every one hour...
      
                      // after that call again your method to schedule again
                      // if you have boolean if the user doesnt want to continue
                      // create a Preference or store it and retrieve it here like
                      boolean mContinue = getUserPreference(USER_CONTINUE_OR_NOT);//
      
                      if(mContinue){
                              scheduleEveryOneHour();
                      } 
              }
      }
      

      希望有帮助:)

      【讨论】:

      • 我有一个dout wakeUpTime.add(Calendar.HOUR, ONE_HOUR);我应该在 One_HOUR 中放什么? 60,000 毫秒 ??
      • 我编辑了我对 Calendar.SECOND 60*60 的回答。您也可以使用 HOURHOUR_OF_DAY ,HOUR 将是 12 小时制,即当前小时 + 1 % 12,或者如果 HOUR_OF_DAY 将是 24 小时制 current_hour + 1 % 24。就是这样。
      【解决方案3】:

      使用 AlarmManager 引用 thistutorial 和 PendingIntent

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-11
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-18
        • 1970-01-01
        • 2021-05-11
        相关资源
        最近更新 更多