Android 1 小时后执行一个函数 [英] Android execute a function after 1 hour

查看:21
本文介绍了Android 1 小时后执行一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an android application where I am storing user's data on database when he/she activates the app. My app requires the user to stop the application manually in order to remove its entry from the database and along with that other services which keep running when the app is activated.

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

So I want to write a function which will be executed after every hour (when the app is activated) and will give a notification to user just to remind him/her about the service which is running .If the user had forgot to stop the service then they can stop it or continue with service.

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

What is the best efficient way of doing this. I dont want to drain too much of battery with thihs 1 hour basis check if the user considers it to run for a day or so. Please advice. Thanks :)

推荐答案

我建议代码是这样的.

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

希望有帮助:)

这篇关于Android 1 小时后执行一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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