Reputation: 19
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.
Upvotes: 1
Views: 295
Reputation: 2577
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.
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.
Upvotes: 2