我正在尝试为用户显示倒数计时器。功能(myfunc)与按钮点击完美配合。但是我希望在创建活动后立即运行它。在Oncreate方法中。但是myfunc没有在OnCreate方法中工作。
以下是MainActivity的代码
public class MainActivity extends AppCompatActivity {
private enum TimerState {
STOPPED,
RUNNING
}
private static final long TIMER_LENGHT = 60; // Sixty seconds
private long mTimeToGo;
private CountDownTimer mCountDownTimer;
private TimerState mState;
@BindView(R.id.main_timer)
TextView mTimerText;
@BindView(R.id.main_timer_button)
Button mTimerButton;
PrefUtils mPreferences;
long startTime;
Calendar rightNow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mState=TimerState.STOPPED;
mPreferences = new PrefUtils(this);
startTime = mPreferences.getStartedTime();
rightNow = Calendar.getInstance();
myfunc();
}
@Override
protected void onResume()
{
super.onResume();
initTimer();
removeAlarm();
}
@Override
protected void onPause() {
super.onPause();
if (mState == TimerState.RUNNING) {
mCountDownTimer.cancel();
setAlarm();
}
}
private long getNow()
{
return rightNow.getTimeInMillis() / 1000;
}
private void initTimer()
{
if (startTime >= 0)
{
mTimeToGo = (TIMER_LENGHT - (getNow() - startTime));
if (mTimeToGo <= 0)
{ // TIMER EXPIRED
mTimeToGo = TIMER_LENGHT;
mState = TimerState.STOPPED;
onTimerFinish();
}
else
{
startTimer();
mState = TimerState.RUNNING;
}
}
else
{
mTimeToGo = TIMER_LENGHT;
mState = TimerState.STOPPED;
}
updateTimeUi();
}
private void onTimerFinish()
{
Toast.makeText(getApplicationContext(),R.string.timer_finished,Toast.LENGTH_LONG).show();
mTimerText.setText(R.string.timer_finished);
mPreferences.setStartedTime(0);
mTimeToGo=TIMER_LENGHT ;
updateTimeUi();
}
private void updateTimeUi()
{
mTimerText.setText(String.valueOf(mTimeToGo));
}
private void startTimer()
{
mCountDownTimer = new CountDownTimer(mTimeToGo*1000 , 1000) {
public void onTick(long millisUntilFinished)
{
mTimeToGo -= 1;
updateTimeUi();
}
public void onFinish()
{
mState = TimerState.STOPPED;
onTimerFinish();
updateTimeUi();
}
}.start();
}
public void myfunc()
{
if (mState == TimerState.STOPPED) {
mPreferences.setStartedTime(getNow());
startTimer();
mState = TimerState.RUNNING;
}
}
public void setAlarm() {
long wakeUpTime = (mPreferences.getStartedTime() + TIMER_LENGHT) * 1000;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, TimerExpiredReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender);
} else {
am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender);
}
}
public void removeAlarm() {
Intent intent = new Intent(this, TimerExpiredReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.cancel(sender);
}
}
这里有一个utils代码
class PrefUtils {
private static final String STARTED_TIME_ID = "com.whiterabbit.time";
private SharedPreferences mPreferences;
PrefUtils(Context c)
{
mPreferences = PreferenceManager.getDefaultSharedPreferences(c);
}
long getStartedTime()
{
return mPreferences.getLong(STARTED_TIME_ID, 0);
}
void setStartedTime(long started) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putLong(STARTED_TIME_ID, started);
editor.apply();
}
}
广播接收器也在这里
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, i, 0);
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
b.setSound(notification)
.setContentTitle(context.getString(R.string.timer_finished))
.setAutoCancel(true)
.setContentText(context.getString(R.string.timer_finished))
.setSmallIcon(android.R.drawable.ic_notification_clear_all)
.setContentIntent(pIntent);
Notification n = b.build();
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, n);
}