简体   繁体   English

哪个实施最佳? 在用户设置的时间后触发代码的两种方法

[英]Which implementation is best? 2 ways to fire code after a user-set amount of time

SCENARIO: 场景:

User enters 45 (minutes) into an EditText, and presses the "OK" Button.. 用户在EditText中输入45(分钟),然后按“确定”按钮。

After 45 minutes has passed, I want to execute a block of Code.. 45分钟过去之后,我要执行一个代码块。

I have been debating between 2 different ways to do this - WHICH IS BEST & WHY? 我一直在讨论两种不同的方法来做到这一点- 最好和为什么?


OPTION #1 - AlarmManager -> PendingIntent -> Intent -> BroadcastListener 选项#1 -AlarmManager-> PendingIntent-> Intent-> BroadcastListener

    int timeValue = Integer.parseInt(editText_timer_value.getText().toString());

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Intent intent = new Intent(this, TimerReceiver.class);

    PendingIntent pendingIntent = PendingIntent
            .getBroadcast(this.getApplicationContext(), 234324243, intent, 0);

    alarmManager.set(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + (timeValue * 60000), pendingIntent);

    finish();

    // TimerReceiver.class fires when the time is up, and contains additional Code.


(OR) (要么)

OPTION #2 - Intent -> intent Extras -> Service -> CountDownTimer 选项#2-意向->意向附加->服务-> CountDownTimer

    String timeValue = editText_timer_value.getText().toString();

    Intent intent = new Intent(this, TimerService.class);

    intent.putExtra("TIMER_VALUE", timeValue);

    getApplicationContext().startService(intent);

    finish();

    // TimerService.class starts immediately and runs a CountDownTimer from intent Extras



SO , Which implementation is "better" or "correct", and WHY? SO ,哪个实现“更好”或“正确”, 为什么?
Thanks! 谢谢! :) :)

PS.. Any other suggestions are very much welcomed as well! PS ..任何其他建议也非常欢迎!

Alarm Manager: This class provides access to the system alarm services. 警报管理器:此类可提供对系统警报服务的访问。 These allow you to schedule your application to be run at some point in the future. 这些使您可以计划您的应用程序在将来的某个时间运行。 When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. 警报响起时,系统会广播已为其注册的Intent,并在目标应用程序尚未运行时自动启动它。 Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted. 设备处于睡眠状态时会保留已注册的警报(如果警报在这段时间内关闭,可以选择将其唤醒),但是如果将其关闭并重新启动,则将被清除。

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. 只要警报接收器的onReceive()方法正在执行,警报管理器就会保持CPU唤醒锁。 This guarantees that the phone will not sleep until you have finished handling the broadcast. 这样可以确保手机在完成广播处理之前不会进入睡眠状态。

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. 注意:警报管理器适用于希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行。 For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler. 对于正常的计时操作(滴答声,超时等),使用Handler会更容易且效率更高。

learn about it more at https://developer.android.com/reference/android/app/AlarmManager.html https://developer.android.com/reference/android/app/AlarmManager.html上了解更多

Service : A service is simply a component that can run in the background even when the user is not interacting with your application. 服务:服务只是一个组件,即使用户不与您的应用程序交互,它也可以在后台运行。 Thus, you should create a service only if that is what you need. 因此,仅在需要时才应创建服务。

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. 如果您需要在主线程之外执行工作,而只是在用户与您的应用程序交互时执行工作,那么您可能应该改为创建一个新线程而不是服务。 For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). 例如,如果您想播放一些音乐,但是只有在您的活动正在运行时,才可以在onCreate()中创建一个线程,在onStart()中开始运行它,然后在onStop()中停止它。 Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. 还可以考虑使用AsyncTask或HandlerThread,而不是传统的Thread类。 See the Processes and Threading document for more information about threads. 有关线程的更多信息,请参见“进程和线程”文档。

learn about service at https://developer.android.com/guide/components/services.html 通过https://developer.android.com/guide/components/services.html了解有关服务的信息

according to the documentation, Android says, you should use service to do really long running, heavy task. 根据文档,Android表示,您应该使用服务来执行长期运行的繁重任务。 Since all you want to use timer for a certain period of time then using a service for that is completely nonsense and waste of resources. 由于您只想在某个时间段内使用计时器,因此为此使用服务完全是胡说八道,浪费资源。

so i suggest you to use AlaramManager, as android says in the documentation. 所以我建议您使用AlaramManager,就像android在文档中说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android用户设置的语言环境总是在onCreate之后重置吗? - Android user-set Locale always get reset after onCreate? 在 React Native 中检测日期更改并在用户设置的时间发送通知 - Detect date changes and send notification at user-set time in React Native 在设定的时间后删除对话框 - Removing a dialog after a set amount of time 如何在 kotlin 中检测用户设置的屏幕缩放级别 - How do I detect the user-set screen zoom level in kotlin Android:使View在定义的时间后消失的最佳方法 - Android: best way to make a View disappear after a defined amount of time 如何在一段时间后删除parse.com中的对象 - How to delete objects in parse.com after a set amount of time 如何设置一定时间后发生的事情? - How to set something to happen after a certain amount of time? Android:哪种是根据用户偏好设置字体大小的最佳方法? - Android: which is the best way to set the font size depending on user preferences? 我的 AlarmManager 可以在达到设定时间时触发第一个警报,而不是在第一次重复之后吗? - Can my AlarmManager fire the first alarm, when the set time is reached, not after the first repeating? 当用户在编辑文本中输入金额时,用金额替换 0 的最佳方法 - Best approach to replace 0 with amount, when user enters an amount in edittext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM