1

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

4

1 回答 1

2

是的,你可以这样做,但目前我没有代码。我可以给你制作你自己的步骤。

脚步

1-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- Create A BroadCastReceiverwith BOOT_COMPLETEDAction 使用 Sharedpreferences 中上次保存的值再次启动 Timer

@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文件中注册您的接收方

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

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

于 2017-01-10T11:49:46.683 回答