简体   繁体   中英

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..

After 45 minutes has passed, I want to execute a block of Code..

I have been debating between 2 different ways to do this - WHICH IS BEST & WHY?


OPTION #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

    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?
Thanks! :)

PS.. Any other suggestions are very much welcomed as well!

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. 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. 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.

learn about it more at 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(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

learn about service at 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM