Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在1小时后执行一个函数_Android_Performance_Timer_Android Asynctask - Fatal编程技术网

Android在1小时后执行一个函数

Android在1小时后执行一个函数,android,performance,timer,android-asynctask,Android,Performance,Timer,Android Asynctask,我有一个android应用程序,当用户激活应用程序时,我将用户的数据存储在数据库中。我的应用程序要求用户手动停止应用程序,以便从数据库中删除它的条目,以及在激活应用程序时保持运行的其他服务 所以我想写一个函数,它将在每小时后执行(当应用程序被激活时),并向用户发出通知,提醒他/她正在运行的服务。如果用户忘记停止服务,那么他们可以停止服务或继续服务 做这件事最有效的方法是什么。我不想消耗太多的电池,以1小时为基础,检查用户是否认为它可以运行一天左右。请给我一些建议。谢谢:)使用AlarmManag

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

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


做这件事最有效的方法是什么。我不想消耗太多的电池,以1小时为基础,检查用户是否认为它可以运行一天左右。请给我一些建议。谢谢:)

使用AlarmManager Reference并使用PendingContent

试试这种方法,希望这能帮助您解决问题。

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

我建议代码是这样的

// 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唤醒时间。添加(Calendar.HOUR,one_HOUR);我应该在一小时内放什么?60000毫秒??我编辑了我对日历的回答。秒60*60。您也可以这样做,或者,小时将是12小时时钟,即当前小时+1%12,或者如果一天中的小时将是24小时时钟,则当前小时+1%24。就这样。