Android计时器应用

时间:2017-01-10 11:33:23

标签: android android-timer

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

1 个答案:

答案 0 :(得分: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-使用BroadCastReceiver创建一个BOOT_COMPLETED操作再次使用您在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>

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