gpt4 book ai didi

安卓定时器应用

转载 作者:行者123 更新时间:2023-11-29 19:25:04 24 4
gpt4 key购买 nike

有没有可能当我设置定时器时,即使我重新启动手机它仍然会运行?就像闹钟一样,当我重新启动手机时它仍然存在。如果可能的话,我能得到它的代码吗?我真的需要它。

最佳答案

是的,你可以做到这一点,但目前我没有代码。我可以为您提供制作自己的步骤。

步骤

1- 在您的 Activity 中创建 CountDownTimer

            // 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- 创建一个 BroadCastReceiver 并使用 BOOT_COMPLETED Action 使用您上次从 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();
}

就是这样。

编辑

第 1 步 - 创建一个 TestActivity.java

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

第 2 步 - 创建 BootReceiver.java

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

第 3 步 - 在 AndroidManifest.xml 文件中注册您的 Receiver

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

这是完整的代码。您只需按照这些步骤操作即可。

关于安卓定时器应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com