Is it anyway possible that when I set a timer, it will still run even if I restart the phone? Like the alarm clock it is still there when I restart the phone. If it is possible can I get the code of it? I really need it.
Asked
Active
Viewed 286 times
1
-
The alarm works cause it add a CalendarEvent, for what you want to achieve, you need to calculate when the timer needs to goes on depending on when it it started and add an event, have a look [here](https://developer.android.com/training/scheduling/alarms.html) – Luca Nicoletti Jan 10 '17 at 11:35
1 Answers
2
Yes you can do this but currently I have not the code. I can give you steps to make your own.
Steps
1- Create CountDownTimer
in your activity
// 10 minutes Timer And 1 Second Delay
new CountDownTimer(10*30*1000, 1000) {
public void onTick(long millisUntilFinished) {
// save `millisUntilFinished` to sharedpreferences
}
public void onFinish() {
// clear sharedPreferences when it finished
//and do whatever you want after finishing the timer here
}
}.start();
2- Create A BroadCastReceiver
with BOOT_COMPLETED
Action start your Timer again with your last saved value from Sharedpreferences
@Override
public void onReceive(Context context, Intent intent) {
//again Start your timer from here
// Get millisUntilFinished from SharedPreference
millisUntilFinished = Long.parseLong(getLastSavedValueFromSharedPreferences());
new CountDownTimer(millisUntilFinished, 1000) {
public void onTick(long millisUntilFinished) {
// save `millisUntilFinished` to sharedpreferences
}
public void onFinish() {
// clear sharedPreferences when it finished
// and do whatever you want after finishing the timer here
}
}.start();
}
That's It.
EDIT
Step 1- Create a TestActivity.java
class
public class TestActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_work);
sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);
editor = sharedPreferences.edit();
startTimer();
}
private void startTimer() {
// 10 min Timer
new CountDownTimer(10*60*1000, 1000)
{
@Override
public void onTick(long millisUntilFinished) {
editor.putLong("millisUntilFinished", millisUntilFinished);
editor.commit();
}
@Override
public void onFinish() {
editor.clear();
// Do your work Here
}
}.start();
}
}
Step 2- Create BootReceiver.java
class
public class BootReceiver extends BroadcastReceiver {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
public void onReceive(final Context context, final Intent intent) {
sharedPreferences = context.getSharedPreferences("MySharedPref", context.MODE_PRIVATE);
editor = sharedPreferences.edit();
startTimer();
}
private void startTimer() {
// get remaining time from sharedPreferences
long millisUntilFinished = sharedPreferences.getLong("millisUntilFinished", 0);
// 10 min Timer
new CountDownTimer(millisUntilFinished, 1000)
{
@Override
public void onTick(long millisUntilFinished) {
editor.putLong("millisUntilFinished", millisUntilFinished);
editor.commit();
}
@Override
public void onFinish() {
editor.clear();
// Do your work Here
}
}.start();
}
}
Step 3- Register your Receiver in AndroidManifest.xml
file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--Register your BootReceiver here-->
<receiver android:name=".receiver.BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Here is the complete code. You just need to follow the steps.

Community
- 1
- 1

Vishal Chhodwani
- 2,567
- 5
- 27
- 40
-
So will the timer still run even if I restart the phone? Like for example I have 10 mins in timer then I only have 5 mins left then I restart the phone. Will the timer be still at 5 Mins? – Glenn Michael Torregosa Jan 10 '17 at 12:13
-
And also can you elaborate about the sharedPreferences part? It's the only thing that I have not understood from the code. – Glenn Michael Torregosa Jan 10 '17 at 12:19
-
@GlennMichaelTorregosa Yes when you restart the phone your Broadcast receiver will called and your timer will start again. And sharedPreferences is the data storing system. it allows you to store and access your data. – Vishal Chhodwani Jan 10 '17 at 12:44
-
Here is the best example of sharedPreferences http://stackoverflow.com/a/23024962/6676466 Try this to store and retrieve your data. – Vishal Chhodwani Jan 10 '17 at 12:45
-
So, when the phone is already restarted then there is no need to run the app again? It will automatically continue to count down? – Glenn Michael Torregosa Jan 10 '17 at 12:58
-
Yes it will automatically run again. The BroadcastReceiver will handle the action of BOOT_COMPLETED. when you restart the phone the BroadcastReceiver called itself. and we have wrote the code of starting Timer in BroadcastReceiver. Here is the [BroadcastReceiver Example](http://stackoverflow.com/a/5290164/6676466) – Vishal Chhodwani Jan 10 '17 at 13:05
-
@GlennMichaelTorregosa I have edited My Answer with complete code Please Check it. – Vishal Chhodwani Jan 10 '17 at 13:26
-
The thing to keep in mind is if the timer should be updated while rebooting. Like what if the timer has 5 min left but the phone is offline for 2 minutes so logically it should continue running with 3 minutes left. If it should continue from 5 minutes this answer is the correct one else I would recommend the comment from @Luca Nicoletti – Nico Jan 10 '17 at 13:35
-
Thanks man! I will test your code tomorrow. Hopefully, it will work like a charm – Glenn Michael Torregosa Jan 10 '17 at 13:35